Home > Back-end >  Refer to the problem of thread pool
Refer to the problem of thread pool

Time:01-15

The following is the main function of
 
The ExecutorService ExecutorService=Executors. NewCachedThreadPool ();
ArrayList ArrayList=new arrayList ();
for (int i=0; I & lt; 5; I++) {
Future StringFuture=executorService. Submit (new Task ());
ArrayList. Add (stringFuture);
}
The executorService. Shutdown ();
//
for (int i=0; I & lt; ArrayList. The size (); I++) {
Try {
System. The out. Println (arrayList. Get (I). The get ());
{} catch InterruptedException (e)
e.printStackTrace();
} the catch (ExecutionException e) {
e.printStackTrace();
}
}

I want to ask about you, about the thread pool behind this my first cycle can speak thread pool to stop, or must be placed behind the second loop, I think that in the first cycle has taken the Task class return value in the Future of the object and then into the collection, and then can traverse a collection of all results,
Then if I don't directly in the first cycle list, print directly as a result, to turn into a single thread, whether can be thought of as the get method must lead to block a thread is a thread after,

CodePudding user response:

Use the shutDown () method to close the thread pool on the first cycle, after shutDownNow () method is washed-up, behind your point of view is wrong, although the first loop ends immediately return to the Future object, but does not represent, the task has been completed your task may also in execution and there is no return value, and not get methods lead to blocking, but if you haven't performed the task is no return value, call the get method to obtain the return value will need to get block to provide return value after the task has been completed

CodePudding user response:

1. In the first for loop can stop a thread pool?
Can, after the first for loop call shutdown (), the method will stop receiving threads and wait for all already submitted to close the thread pool threads has been completed! Obviously five threads has been submitted, so five threads can normal execution returns! ShutdownNow and shutdown () (), shutdownNow () whether or not in the thread pool threads have already submitted, will interrupt! So when there is a task at run time, shutdownNow () will be an error!
2. The first loop will print directly the results into a single thread?
First cycle will submit a task to the thread pool, program continues to run, if the printing results quite so call FutureTask the get method, will block the main thread, until the end of the task to run, call on the second circulation again, task to be executed simultaneously, the bottom is to call LockSupport. Park, LockSupport. Unpark implementation, the following code! anyway, the question is very good!
 import Java. Util. ArrayList; 
Import the Java. Util. Concurrent. *;

/* *
* @ author: jiaolian
* @ date: Created in the 2021-01-14 09:41
* @ description: refer to the problem of thread pool
* @ modified By:
* public: call practice
*/
Public class ExecutorServiceTest {

Private static class Task implements Callable {

@ Override
Public String call () throws the Exception {
Thread.sleep (3000);
Return the Thread. CurrentThread (). The getName () + ": practice execution call!" ;
}
}
Public static void main (String [] args) throws ExecutionException, InterruptedException {

The ExecutorService ExecutorService=Executors. NewCachedThreadPool ();
ArrayList ArrayList=new arrayList ();
for (int i=0; I & lt; 5; I++) {
Future StringFuture=executorService. Submit (new Task ());
System. The out. Println (stringFuture. The get ());
}
The executorService. Shutdown ();
//
for (int i=0; I & lt; ArrayList. The size (); I++) {
Try {
System. The out. Println (arrayList. Get (I). The get ());
{} catch InterruptedException (e)
e.printStackTrace();
} the catch (ExecutionException e) {
e.printStackTrace();
}
}

}
}
  • Related