PrimeGenerator implements Runnable { static List<BigInteger> aSecondOfPrimes() throws InterruptedException { PrimeGenerator generator = new PrimeGenerator(); exec.execute(generator); try { SECONDS.sleep(1); } finally { generator.cancel(); } return generator.get(); }  void run(); void cancel(); synchronized List<BigInteger> get();  }
@Test public void primeGeneratorTest() { try { List<BigInteger> lst = PrimeGenerator.aSecondOfPrimes(); lst.stream().forEach(val -> System.out.println(val)); } catch (InterruptedException e) { e.printStackTrace(); } }