Java FutureTask Example

I spent a while looking for a solid example of futuretask use in java for my latest android app (more posts on that soon I hope) but I found a great one here

Here is the code:

{.js name="code"} public void startMyApplication() { ExecutorService executor = Executors.newFixedThreadPool(2); FuturTask futureOne = new FutureTask(myFirstProcess); FuturTask futureTwo = new FutureTask(mySecondProcess); executor.execute(futureOne); executor.execute(futureTwo); while (!(futureOne.isDone() && futureTwo.isDone())) { try { // I wait until both processes are finished. Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } logger.info("Processing finished"); executor.shutdown(); // Do some processing on results ... }

 

Back to top

Connect with me

©2016 matt-reid.co.uk