Parallel and parallel arrays of new Java 8 features (eight villains-8)

Source: Internet
Author: User

Jody Domingre Jody

"How do you doing, dummy?" Are you all right, fool?

First, the basic introduction

Java8 not only added stream, but also added parallelstream (parallel stream). In addition to parallel streams, JAVA8 provides simple parallel functionality for normal arrays. Array sorting, generally using the Arrays.sort () method serial sorting, Java8 new Method Arrays.parallelsort () parallel sorting.

Second, use

1, Parallelsort ()

Java 8 Adds a number of new methods to support parallel array processing. The most important is probably parallelsort () This method significantly speeds up sorting on multicore computers. The following small example demonstrates the behavior of this new method (parallelxxx).

This small piece of code uses the parallelsetall () T method to populate the array with a length of 2000, and then use parallelsort () to sort. This program outputs 10 numbers before and after sorting to verify that the array is really sorted. Examples of possible outputs are as follows (please note that these numbers are randomly generated)

Import Java.util.arrays;import java.util.concurrent.ThreadLocalRandom; public class Parallelarrays {public    static void Main (string[] args) {        long[] Arrayoflong = new Long [20000];< C3/>arrays.parallelsetall (Arrayoflong,            Index, Threadlocalrandom.current (). Nextint (1000000));        Arrays.stream (Arrayoflong).. forEach (            i-System.out.print (i + ""));        System.out.println ();         Arrays.parallelsort (Arrayoflong);        Arrays.stream (Arrayoflong).. forEach (            i-System.out.print (i + ""));        System.out.println ();    }}

2. Parallel Stream usage

// Group groups based on specific criteria (such as prime and non-prime)  
. Parallelstream (). Collect (Collectors.groupingby (S-utility.isprime (s));
Integer[]  //array for filtering    . ToArray ();

Third, performance testing

How is it performing?

To test the performance of these parallel operations APIs, I experimented in two scenarios, low competition and high competition. The reason is that running a multi-accounting method alone often has good performance, but running in a real-world server environment is a completely different situation. In real-world environments there are often a large number of threads competing for valuable CPU time slices to process messages or user requests, and the performance of the program is reduced due to the presence of competition. So I carried out the next test. I first randomly generated an array of integers of length 100K, with values from 0 to 1 million. Then I used the traditional sequential method and the new Java 8 Parallel API to sort, group, and filter the array. The results are not surprising.

    • Quick Sort Fast 4.7 times
    • 5 times faster than a group .
    • filtration is 5.5 times faster.

Does this indicate that Java 8 's parallel API has very good performance? Unfortunately, I can't.

* The test results are consistent with the additional test results running 100 times.
* Test machine for mbp,i7 four cores.

What happens when there is a load?

The performance of the new API so far has been excellent because there is very little competition between threads for CPU time slices. This is the ideal environment, but not the ideal environment often does not appear in the real environment. In order to simulate the real environment, I set up a second Test. This test uses the same algorithm as the first time, but the test task executes on 10 concurrent threads to simulate a situation where the server in a stressful environment processes 10 requests simultaneously. These 10 requests are handled using a traditional smooth processing method or a new API for Java 8.

Test Results
    • sort of now only 20% faster
    • Filtration is now only 20% faster.
    • The group is now 15% full.

Higher scale and competitive levels are likely to further reduce these figures. The reason is that adding threads in a multithreaded environment does not necessarily help you increase computational efficiency, but the number of CPUs in a computer determines the computational efficiency, not the number of threads.

Conclusion

While these are very powerful and easy-to-use APIs, they are not silver bullets. We still need to spend our energies to determine when we should use them. If you knew beforehand that you would do multiple processing parallel operations, it might be a good idea to consider using a queuing schema and matching the number of concurrent operands to your processor. The difficulty here is that runtime performance will depend on the actual hardware architecture and the server's stress situation. You may only be able to see the runtime performance of your code in a stress test or production environment, making it a classic case of "easy to code, difficult to debug."

Reference Links:

Http://www.importnew.com/11113.html

Image source: Eight villains (movie)

Parallel and parallel arrays of new Java 8 features (eight villains-8)

Related Article

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.