MolecularFormulaGenerator implements IFormulaGenerator { @Override public void cancel() { formulaGenerator.cancel(); } MolecularFormulaGenerator(final IChemObjectBuilder builder,
                                     final double minMass, final double maxMass,
                                     final MolecularFormulaRange mfRange); @Override IMolecularFormula getNextFormula(); @Override IMolecularFormulaSet getAllFormulas(); @Override double getFinishedPercentage(); @Override void cancel();  }
@Test(timeout = 1000) public void testCancel() throws Exception { IsotopeFactory ifac = Isotopes.getInstance(); IIsotope c = ifac.getMajorIsotope("C"); IIsotope h = ifac.getMajorIsotope("H"); IIsotope n = ifac.getMajorIsotope("N"); IIsotope o = ifac.getMajorIsotope("O"); IIsotope p = ifac.getMajorIsotope("P"); IIsotope s = ifac.getMajorIsotope("S"); MolecularFormulaRange mfRange = new MolecularFormulaRange(); mfRange.addIsotope(c, 0, 1000); mfRange.addIsotope(h, 0, 1000); mfRange.addIsotope(o, 0, 1000); mfRange.addIsotope(n, 0, 1000); mfRange.addIsotope(p, 0, 1000); mfRange.addIsotope(s, 0, 1000); double minMass = 100000.0; double maxMass = 100000.001; final MolecularFormulaGenerator gen = new MolecularFormulaGenerator( builder, minMass, maxMass, mfRange); Runnable cancelThread = new Runnable() { @Override public void run() { try { Thread.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } gen.cancel(); } }; new Thread(cancelThread).run(); gen.getAllFormulas(); IMolecularFormula f = gen.getNextFormula(); Assert.assertNull(f); }