Futuretask implements the runnable and future interfaces and is an asynchronous task that can be canceled. This class provides a basic implementation of the future, taking advantage of the method of starting and canceling the calculation, the method of querying whether the calculation is complete, and the method of obtaining the result of the calculation. The result is only available when the calculation is complete, and if the calculation is not completed, the Get method is blocked. Once the calculation is complete, you cannot start or cancel the calculation again. It is more important to implement a callback method that completes a task. This is good.
@Test Public voidtest2 () {myfuturetask task=NewMyfuturetask (NewCallable<string>() {@Override PublicString Call ()throwsException {return"Gaoxing"; } }); NewThread (Task). Start (); }}classMyfuturetaskextendsfuturetask{ PublicMyfuturetask (callable arg0) {Super(arg0); } protected voidDone () {Try{System.out.println ( This. Get ()); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(executionexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
Java Concurrency Futuretask