Java 5 multithreading-Thread Pool

Source: Internet
Author: User

1. the Java 5 thread pool concept and the Executors class applications first introduce the programming model principles on the TCP server. When a client is connected to the server, the server needs to assume a thread server, when the client's session ends, the thread ends. That is, the server creates a new thread for each client connection, this is like assuming that each registered student needs to be answered by me in order to give each student a good feeling, but it takes half an hour for each student to register, for 50 students, what should I do if I handle the reception and handling procedures separately is obviously not practical? I will receive each student first. After greeting me, I will assign him to a staff member to go through the formalities, so that I will receive each student. if there are many clients accessing the server, the server must constantly create and destroy threads, which will seriously affect the server performance, it is also impossible for us to arrange a new employee to serve it. Isn't it necessary for the company to recruit a lot of staff. A staff member should have served a trainee. After being idle, once a new trainee wants to serve, we will arrange the trainee to serve it. the thread pool concept is similar. First, some threads are created. Their set is called the thread pool. After the server receives a client request, A idle thread is taken from the thread pool for the service. After the service is completed, the thread is not closed, but returned to the thread pool. in the programming mode of the thread pool, the task is handed over to the entire thread pool, rather than directly to a thread. When the thread pool obtains the task, it finds a idle thread internally, then, the task is handed over to an internal idle thread, Which is encapsulation. remember, the task is handed over to the entire thread pool, but multiple tasks can be submitted to one thread pool at the same time. create a fixed-size thread pool: ExecutorService threadPool = executors.newfixedthreadpool(3%%threadpool.exe cute (Runnable runnable); Create a cache thread pool: if the thread is not enough to automatically create a new thread to meet the service ExecutorService threadPool = executors.newcachedthreadpool(zookeeper threadpool.exe cute (Runnable runnable); create a single thread pool. there is only one thread in the thread. If the thread unexpectedly dies, the system will automatically create a new thread to replace it. executorService threadPool = executors.newsinglethreadexecutor(threadpool.exe cute (Runnable runnable); // close the thread pool after the task is executed, threadPool. shutdown (); // no matter whether the task is completed or not, the threadPool of the thread pool is closed. shutdownNow (); Use the thread pool to start the timer. executors. newScheduledThreadPool (3 ). schedule (new Runnable () {@ Overridepublicvoid run () {}}, 20, TimeUnit. SECONDS); 2. Callable and Future use these two classes to get the results returned after the thread is executed. executorService executorService = Executors. newSingleThreadExecutor (); // submit the task Future using the submit method <String> future = executorService. submit (new Callable <String> () {@ Override public String call () throws Exception {return "hello world" ;}}); System. out. println ("waiting for returned results"); try {System. out. println ("returned result," + future. get ();} catch (Exception e) {e. printStackTrace ();} The result type returned by Future must be the same as that returned by Callable. This is achieved through generics. the Future object returned by the submit () method can cancel the task. completionService is used to submit a set of Callable tasks. Its take () method returns the Future object ExecutorService executorService2 = Executors corresponding to a completed Callable task. newFixedThreadPool (3); CompletionService <Integer> service = new ExecutorCompletionService <Integer> (executorService2); for (int I = 0; I <10; I ++) {final int k = I; service. submit (new Callable <Integer> () {@ Override public Integer call () throws Exception {Thread. sleep (new Random (). nextInt (5000); return k ;}}) ;}try {for (int I = 0; I <10; I ++) {System. out. println (service. take (). get () ;}} catch (Exception e) {e. printStackTrace ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.