In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What is the essence and function of Java thread pool, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
Friends who are familiar with java multithreading must know a lot about java's thread pool, and the core implementation class in jdk is java.util.concurrent.ThreadPoolExecutor. You may know how it works, or even read its source code, but like me, you may have misunderstandings about what it does. Now the question is, why does jdk provide Java thread pools? What is the advantage of using java thread pools to create a new Thread every time?
Misunderstanding of thread pool
For a long time I thought that java thread pools were designed to improve the efficiency of creating threads in multithreading. Some threads are created and cached in the thread pool, and then the request (Runnable) is pulled out of the connection pool to process the request; this avoids creating a new Thread object one at a time. Until some time ago I saw an interview with Neal Gafter (co-author of "Java Puzzlers" with Joshua Bloch, now at Microsoft, mainly working on the .NET language), in which there was a conversation (http://www.infoq.com/cn/articles/neal-gafter-on-java):
At first glance, the big god's thinking is different: Java thread pooling is to prevent java threads from taking up too much resources?
Although it is an interview with the great god java, you can't believe everything. You say that resources take up resources? You still have to write a test case.
First of all, verify my understanding:
Which is more efficient, Java thread pooling or creating java threads?
Go directly to the test case:
Public class ThreadPoolTest extends TestCase {private static final int COUNT = 10000; public void testThreadPool () throws InterruptedException {CountDownLatch countDownLatch = new CountDownLatch (COUNT); ExecutorService executorService = Executors.newFixedThreadPool (100); long bg = System.currentTimeMillis (); for (int I = 0; I < COUNT; iTunes +) {Runnable command = new TestRunnable (countDownLatch); executorService.execute (command);} countDownLatch.await () System.out.println ("testThreadPool:" + (System.currentTimeMillis ()-bg));} public void testNewThread () throws InterruptedException {CountDownLatch countDownLatch = new CountDownLatch (COUNT); long bg = System.currentTimeMillis (); for (int I = 0; I < COUNT; iTunes +) {Runnable command = new TestRunnable (countDownLatch); Thread thread = new Thread (command); thread.start () } countDownLatch.await (); System.out.println ("testNewThread:" + (System.currentTimeMillis ()-bg));} private static class TestRunnable implements Runnable {private final CountDownLatch countDownLatch; TestRunnable (CountDownLatch countDownLatch) {this.countDownLatch = countDownLatch;} @ Override public void run () {countDownLatch.countDown ();}
Executors.newFixedThreadPool (100) is used here to control the number of core connections in the thread pool as large as the number of * connections, both 100.
The test results on my machine:
TestThreadPool:31
TestNewThread:624
As you can see, the processing time for 10000 requests using the thread pool is 31ms, while the processing time for each new thread enabled is 624ms.
Well, it's really faster to use a thread pool than to create a new thread every time, but testNewThread takes a total of 624ms, which calculates the average time spent per request:
624ms/10000=62.4us
The time to create and start a thread is 62.4 microseconds each time. According to the 80apper20 principle, this time is negligible at all. So thread pools are not designed for efficiency.
Java thread pool is to save resources?
Go back to the test case:
Public class ThreadPoolTest extends TestCase {public void testThread () throws InterruptedException {int I = 1; while (true) {Runnable command = new TestRunnable (); Thread thread = new Thread (command); thread.start (); System.out.println (iTunes +);} private static class TestRunnable implements Runnable {@ Override public void run () {try {Thread.sleep (1000) } catch (InterruptedException e) {e.printStackTrace ();}
The above use case simulates that a new thread is created for each request to process the request, and then the default processing time for each request is 1000ms. On my machine, memory overflows when the number of requests reaches 1096:
Java.lang.OutOfMemoryError: unable to create new native thread
Why did you throw OOM Error? Because jvm allocates a certain amount of memory for each thread (after JDK5.0, the stack size of each thread is 1m, and the previous stack size of each thread is 256K, which can also be set by the jvm parameter-Xss), the error is reported when the number of threads reaches a certain number.
Imagine that if, instead of using a java thread pool, a new thread is created for each request to process the request, memory will be spilled when the number of requests reaches a certain number; and if we use the java thread pool, the number of threads must be
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.