In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to change the ordinary Thread multithreading to Java8 parallelStream concurrent stream, the content of the article is of high quality, so the editor will share it for you to do a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Summary and summary
Java8's parallelStream concurrent stream can achieve a similar effect with multithreading, but it is not good either. In order to get a similar effect with the previous version of multithreading, it has been changed again and again, although it has been changed at last, but there are still some incomprehensible places.
How to get a parallelStream
In theory, you need to have a List, any type of List, and then call its .parallelStream () method.
For my example, the type of the element is not important, so the Integer type is chosen, and the core code is as follows:
AtomicInteger atomicInteger = new AtomicInteger (0); return Arrays.asList (new Integer [size]). ParallelStream (). Map (I-> atomicInteger.incrementAndGet ())
It is worth noting that the first line uses AtomicInteger instead of Integer because Integer has concurrency problems
The second line means: create a new array of size size, convert the array to List, convert List to parallelStream, initialize the elements in the list into incremented integers, and finally return.
Why is parallelStream not a good stubble?
To put it simply, I think the reason is: because its default value is CPU-intensive, while the average Web project is IO-intensive (general Web projects need to deal with the database, the operation against the database is mainly IO, and the consumption of CPU is not high).
When default values cannot be used, developers need to learn more about the use of parallelStream, which is not particularly easy to find. For example: what is the default number of concurrent threads for parallelStream? How to modify the default number of threads?
I finally found this question and answer: Custom thread pool in Java 8 parallel stream for reference.
Question1: how do I get the number of CPU processors available in Java code? The code is as follows: (on my machine 8 core, the result is: 8)
Runtime.getRuntime () .availableProcessors ()
Question 2: what is the default number of concurrent threads for parallelStream? The code is as follows: (on my machine, the result is: 7)
ForkJoinPool.getCommonPoolParallelism ()
Question 3: why is the default number of concurrent threads in parallelStream less than the number of CPU processors?
Because the optimal policy is to allocate one thread per CPU processor, however, the main thread is also a thread, so there is a quota.
Question4: what if the computer is poor and there is only one CPU? Anyway, the default number of concurrent threads is 1, which can't be zero.
Question 5: the default number of concurrent threads is too small, how to modify it? The code is as follows: (changed to 20)
System.setProperty ("java.util.concurrent.ForkJoinPool.common.parallelism", "20")
Question 6: can the default number of concurrent threads be modified repeatedly? I can't. Because java.util.concurrent.ForkJoinPool.common.parallelism is of type final, setting is allowed only once in the entire JVM.
Execute the following code:
Int astat5 break; for (;) {System.setProperty ("java.util.concurrent.ForkJoinPool.common.parallelism", "" + aura +); System.out.println ("ForkJoinPool.getCommonPoolParallelism ():" + ForkJoinPool.getCommonPoolParallelism ()); if (a > 7) break;} / * * result:ForkJoinPool.getCommonPoolParallelism (): 5ForkJoinPool.getCommonPoolParallelism (): 5ForkJoinPool.getCommonPoolParallelism (): 5 percent /
Question7: since the default number of concurrent threads cannot be modified repeatedly, how to test concurrency with different number of threads? The answer is to introduce ForkJoinPool. The usage is as follows:
New ForkJoinPool (threadCount) .submit (()-> {parallelStream.forEach (I-> {/ / the code for submitting the order is omitted here});}) .get ()
Question 8: what is the relationship between java.util.concurrent.ForkJoinPool.common.parallelism and new ForkJoinPool (threadCount)? The answer is: no.
The answer was very disappointing, but I really didn't find it out. The results of my test are as follows:
1. If the value of java.util.concurrent.ForkJoinPool.common.parallelism is not set before new ForkJoinPool (threadCount), then the effect of new ForkJoinPool (threadCount) is not obvious, that is, changing the value of threadCount has little impact on performance.
two。 If you previously set the value of java.util.concurrent.ForkJoinPool.common.parallelism, but set it relatively small (such as 32), then the effect of the subsequent new ForkJoinPool (threadCount) is not obvious.
3. Only when the value of java.util.concurrent.ForkJoinPool.common.parallelism is set to a large value (for example, 10000), and then the threadCount in new ForkJoinPool (threadCount) is changed, will it have a significant impact on performance.
Question9: what if you modify it as in question 8 and set the value of java.util.concurrent.ForkJoinPool.common.parallelism to a large value (such as 10000), which means that it is no longer suitable for CPU-intensive operations?
The answer is: use new ForkJoinPool (threadCount) every time and discard the default parallelStream as a whole. How troublesome that is
You see, there are so many problems casually that you don't want to use them, don't you?
The relationship between the number of concurrent threads and the number of orders that can be submitted per second
The comparison between the results of this test and the results of the last test is as follows: (red is the result of the previous version using Thread, and black is the result of this version using parallelStream)
You can see that there is not much difference.
[note]: the test results on different machines will be different, the above test results are for reference only.
On how to change the ordinary Thread multithreading to Java8 parallelStream concurrent stream to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.