In the project in order to improve the performance stability of large concurrency, often use the thread pool to do multi-threaded asynchronous operations, there are 2 of multithreading, one is to implement the Runnable interface, this has no return value, one is to implement the callable interface, this has a return value.
When one of the lines is blocks until those, it should theoretically not affect the execution of the other threads, but the problem in the project indicates that one thread is blocked and the other threads return an empty interface. It was a very simple question, but I thought about it for the first time. It is simply because the blocked thread is not released, the amount of concurrency is large, and the number of thread pools is full, so the other threads are in a wait state.
Attach a section of your own debugging code, when you want to do not have problems, their own simulation of writing, maybe the problem comes out.
Importjava.util.concurrent.Callable;Importjava.util.concurrent.ExecutionException;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;Importjava.util.concurrent.Future;ImportJava.util.concurrent.TimeUnit;Importjava.util.concurrent.TimeoutException; Public classfuturetest{ Public Static voidMain (string[] args)throwsinterruptedexception, Executionexception, timeoutexception {FinalExecutorservice exec = Executors.newfixedthreadpool (1); Callable<String> call =NewCallable<string>() { PublicString Call ()throwsinterruptedexception {//start a time-consuming operationThread.Sleep (1000 * 2); return"1 Thread execution completed."; } }; Callable<String> call2 =NewCallable<string>() { PublicString Call ()throwsException {//start a time-consuming operation//Thread.Sleep (+ 5); return"2 thread execution completed."; } }; Callable<String> Call3 =NewCallable<string>() { PublicString Call ()throwsException {//start a time-consuming operation//Thread.Sleep (+ 5); return"3 Thread execution completed."; } }; Future<String> future =Exec.submit (call); Future<String> future3 =Exec.submit (CALL3); Future<String> Future2 =Exec.submit (CALL2); String obj=""; String Obj2=""; String obj3=""; Try{obj= Future.get (timeunit.milliseconds);//task processing time-out is set to}//1 seconds Catch(Exception e) {System.out.println ("Processing timed out ...."); E.printstacktrace (); } Try{obj3= Future3.get (timeunit.milliseconds);//task processing time-out is set to}//1 seconds Catch(Exception e) {System.out.println ("Processing timed out ...."); E.printstacktrace (); } Try{obj2= Future2.get (3000, timeunit.milliseconds);} Catch(Exception e) {System.out.println ("Processing timed out ...."); E.printstacktrace (); } System.out.println ("3 Task successfully returned:" +obj3); System.out.println ("2 task successfully returned:" +obj2); System.out.println ("1 task successfully returned:" +obj); Exec.shutdown (); } }
Service exceptions due to asynchronous multithreaded timeouts in Java