I. Use of the future
Futuretask is a combination of the future and the callable. That's what the traditional code says.
Future F = executor.submit (new callable ());
And then the future to get the results of the calculation. However, if multiple tasks are turned on, we don't know which task is the first to end. Therefore, to achieve "when a task at the end, do something immediately, such as logging" This function, you need to write some extra code. For example, general future use.
Multi-tasking:
1 PackageZmx.multithread.test.reentrantlock;2 3 4 ImportJava.util.Random;5 Importjava.util.concurrent.Callable;6 Importjava.util.concurrent.ExecutionException;7 ImportJava.util.concurrent.ExecutorService;8 Importjava.util.concurrent.Executors;9 Importjava.util.concurrent.Future;Ten ImportJava.util.concurrent.FutureTask; One ImportJava.util.concurrent.TimeUnit; A - Public classT1 { - Public Static voidMain (string[] args)throwsinterruptedexception, executionexception { the -Executorservice executor2= Executors.newfixedthreadpool (5); - classTaskImplementsCallable<string>{ - @Override + PublicString Call ()throwsException { - +Random Rand =NewRandom (); ATimeUnit.SECONDS.sleep (Rand.nextint (10)); at returnThread.CurrentThread (). GetName (); - } - } - -list<future<string>> results =NewArraylist<future<string>>(); - for(inti=0;i<5;i++){ infuture<string> f = executor2.submit (NewTask ()); - Results.add (f); to } + - BooleanFlag =true; the while(flag) { * $ for(Iterator<future<string>> iter =results.iterator (); Iter.hasnext ();) {Panax NotoginsengFuture<string> f =Iter.next (); - if(F.isdone ()) { the System.out.println (F.get ()); + Iter.remove (); A the } + } - if(Results.size () ==0){ $Flag =false; $ } - - } the -System.out.println ("Execution Complete");Wuyi the Executor2.shutdownnow (); - Wu - About } $ -}
Execution Result:
Pool-1-thread-4
Pool-1-thread-2
Pool-1-thread-1
Pool-1-thread-5
Pool-1-thread-3
Execution complete
Second, Futuretask
The above uses traversal to solve multi-task results, but is not the optimal effect, Futuretask is the existence of this, it has a callback function protected void done (), when the task ends, the callback function will be triggered. So simply overloading the function allows you to do something at the end of the thread.
The code is as follows:
1 Public classTest {2 Public Static voidMain (string[] args) {3Executorservice executor =Executors.newcachedthreadpool ();4 for(inti=0; i<5; i++) {5callable<string> C =NewTask ();6Myfuturetask ft =NewMyfuturetask (c);7 executor.submit (ft);8 }9 Executor.shutdown ();Ten } One A } - - classMyfuturetaskextendsFuturetask<string> { the - PublicMyfuturetask (callable<string>callable) { - Super(callable); - } + - @Override + protected voidDone () { A Try { atSystem.out.println (Get () + "thread execution finished!" ~"); -}Catch(Interruptedexception |executionexception e) { - //TODO auto-generated Catch block - e.printstacktrace (); - } - } in - to } + - classTaskImplementsCallable<string> { the * @Override $ PublicString Call ()throwsException {Panax NotoginsengRandom Rand =NewRandom (); -TimeUnit.SECONDS.sleep (Rand.nextint (12)); the returnThread.CurrentThread (). GetName (); + } A}
The results are as follows:
Pool-1-thread-4 thread execution is complete!
Pool-1-thread-3 thread execution is complete!
Pool-1-thread-5 thread execution is complete!
Pool-1-thread-2 thread execution is complete!
Pool-1-thread-1 thread execution is complete!
A brief analysis of Java thread futuretask and future