java - Futuretask doesn't work -
i created futuretask
in analog way presented in brian goetz's book java concurrency in practice (the code sample can found here, listing 5.12).
the problem task times out if given 10 seconds. task returns true
there shouldn't reason happen:
public static void main(string[] args) throws exception { futuretask<boolean> task = new futuretask<>(new callable<boolean>() { @override public boolean call() throws exception { return true; } }); system.out.println(task.get(10, timeunit.seconds)); }
this code prints:
exception in thread "main" java.util.concurrent.timeoutexception @ java.util.concurrent.futuretask.get(unknown source) @ main.main(main.java:19)
you haven't executed task. there never result available. javadoc states
this class provides base implementation of
future
, with methods start , cancel a computation, query see if computation complete, , retrieve result of computation. the result can retrieved when computation has completed
submit task executorservice
run asynchronously.
executors.newsinglethreadexecutor().submit(task); // ideally shutdown executorservice afterwards
or run synchronously
task.run();
in links you've given, i'm assuming start()
method runs futuretask
in new thread
called before attempting result.
Comments
Post a Comment