When using the Java thread pool, the return value type is received with future<t>, for example:
future<string> future = Fixedthreadpool.submit (New Thread ());
The returned result must be completed by the thread before it is returned, and we can take advantage of this to thread the Plug and monitor the end time of the thread.
Package Com.test;import Java.util.arraylist;import java.util.list;import java.util.concurrent.callable;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import java.util.concurrent.Future; Public classTestDemo9 {executorservice Indes=Executors.newcachedthreadpool (); Public StaticExecutorservice Fixedthreadpool = Executors.newfixedthreadpool (3); Public Static voidMain (string[] args) {Try { NewTestDemo9 (). Test (); } Catch(Exception e) {System. out. println ("have errors"); } System. out. println ("end of this process execution"); } Public voidtest () throws exception{List<Future<String>> list =NewArraylist<>(); for(inti =0; I <Ten; i + +) { future<String> future = Indes.submit (NewTestDemo9 ().NewTest9 (i)); List.add (future); } System. out. println ("Join Queue End"); /*For (future<string> fu:list) {System.out.println (Fu.get ()); if (Fu.get (). Equals ("Future set 7")) {System.out.println ("the thread at this time has finished executing"); Break } }*/ //System.out.println (fixedthreadpool.isterminated ()); //list.clear (); //System.out.println ("Size" +list.size ()); //Fixedthreadpool.shutdown ();System. out. println ("End of collection printing"); //System.out.println (Fixedthreadpool.isshutdown ()); } classTEST9 Implements Callable<string>{ Private intcount; PublicTest9 (intcount) { This. Count =count; } @Override PublicString Call () throws Exception {if(Count = =1) {System. out. println ("The first process starts running"); } //Thread.Sleep (5000); //list<future<string>> fut = new arraylist<> ();future<string> f = TestDemo10.fixedThreadPool.submit (NewTest10 (count)); String s="Future Collection"; System. out. println ("Execution"+count); S= S+count; if(Count = =1) {System. out. println (F.Get()); System. out. println ("1!!!!! End of execution"); } returns; } } classTEST10 Implements Callable<string>{ Private intcount; PublicTest10 (intcount) { This. Count =count; } @Override PublicString Call () throws Exception {if(Count = =1) {System. out. println ("Start Execution 1111111"); } thread.sleep ( the); String s="Future Collection"; System. out. println (count); S= S+count; if(Count = =1) {System. out. println ("1 start sleeping for 5 seconds"); Thread.Sleep ( the); System. out. println ("1 after sleeping,"); } System. out. println (count+"End of execution"); returns; } }}
As the test code shows, the thread is not finished and will pass through the System. Out.println (f.get()); Blocking, this method does not end, and it can be used to monitor the execution time of the thread.
If you remove System. This method ends immediately after the out.println (f.get()) queue is added.
Personal test, hope the great God guidance!
Java thread pool with self-monitoring, thread pool return value control