In the project in order to increase the performance stability of large concurrency, often use to thread pool to do multithreading asynchronous operation, multithreading has 2 kinds, one is to implement Runnable interface, this has no return value, one is to implement callable interface, this has the return value.
When one of the lines is blocks until those, it should theoretically not affect the execution results of other threads, but the problems that occur in the project indicate that one thread is blocked and the interfaces returned by other threads are empty. It was a very simple question, but I thought about it for the first time. It's simple, because the blocked line
Process is not released, a large amount of concurrency, the number of thread pool is full, so the other threads are in the waiting state.
Attach a section of your own debugging code, when you think there is no problem, the simulation of their own writing, perhaps the problem came out.
Import java.util.concurrent.Callable;
Import java.util.concurrent.ExecutionException;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.Future;
Import Java.util.concurrent.TimeUnit;
Import java.util.concurrent.TimeoutException; public class Futuretest {public static void main (string[] args) throws Interruptedexception, Executionexception, T
imeoutexception {final Executorservice exec = Executors.newfixedthreadpool (1);
Callable<string> call = new Callable<string> () {public String call () throws Interruptedexception
{//Start time-consuming Operation Thread.Sleep (1000 * 2);
Return "1 Thread execution complete."
}
};
callable<string> call2 = new callable<string> () {public String call () throws Exception {
Start time consuming Operations//thread.sleep (1000 * 5);
Return "2 thread execution complete."
}
}; Callable<string> Call3 = new callable<String> () {public String call () throws Exception {//Start time-consuming operation//thread.sleep (1000 *
5);
Return "3 Thread execution complete."
}
};
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 timeout set to}//1 seconds catch (Exception e) {Sy
STEM.OUT.PRINTLN ("Processing timed out ...");
E.printstacktrace (); try{obj3 = Future3.get (3000, timeunit.milliseconds);//Task processing timeout is set to}//1 seconds catch (E
Xception 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 The Task returns successfully:" + obj3);
System.out.println ("2 The Task returns successfully:" + obj2);
SYSTEM.OUT.PRINTLN ("1 Task returns successfully:" + obj);
Exec.shutdown (); }
}
The above is a small series for everyone to talk about Java asynchronous multithreading timeout caused by the service exception all the content, I hope that we support cloud-Habitat Community ~