In our development process, we all know that want to improve the efficiency of the program, we can enable multi-threading to parallel processing, and java8 in the data processing also provides its parallel method, today to learn a simple java8 in the parallel stream and sequential flow.
A parallel stream is one that divides a piece of content into chunks of data and processes each stream of data blocks separately with different threads.
The parallel stream is optimized in Java8, and we can easily parallelize the data. The stream API can declaratively switch between a parallel stream and a sequential stream through parallel () and scqucntial ().
First, the Fork-join framework
Fork-join Framework: Is JAVA7 provide a framework for the implementation of the task, that is, if necessary, a large task, split (Fork) into a number of small tasks (split to no longer split), and then a small task to calculate the results of the join summary.
The Fork-join framework is a concrete implementation of the Executorservice interface to help make better use of multiprocessor benefits. It is designed for work types that can be split recursively into subtasks. The goal is to be able to use all available computing power to improve the performance of your application.
The Fork-join framework distributes tasks to worker threads in the thread pool. The Fork-join framework is unique in its use of work-stealing (work-stealing) algorithms. A worker thread that completes its work and is idle can steal waiting task execution from other worker threads that are still busy (busy), and each worker thread has its own work queue, which is accomplished using a double-ended queue (deque). When a task divides a new thread, it pushes itself to the head of the deque. When the thread's task queue is empty, it tries to steal another task from the tail of another thread's deque.
Below, let's write a simple example to illustrate:
1 /**2 * To use Fark-join, the class must inherit Recursiveaction (no return value) or3 * Recursivetask (with return value)4 * @authorwuyouxin5 *6 */7 Public classForkjoinextendsRecursivetask<long>{8 9 /**Ten * Serialization One */ A Private Static Final LongSerialversionuid = -645248615909548422l; - - Private Longstart; the Private Longend; - - PublicForkjoin (LongStartLongend) { - This. Start =start; + This. end =end; - } + A Private Static Final LongTHRESHOLD = 10000L; at /** - * Override Method - */ - @Override - protectedLong Compute () { - if(End-start <=THRESHOLD) { in Longsum = 0; - for(Longi = start; I < end; i++) { toSum + =i; + } - returnsum; the}Else { * LongMiddle = (End-start)/2; $Forkjoin left =Newforkjoin (start, middle);Panax Notoginseng //split a molecule task, press into thread queue - left.fork (); theForkjoin right =Newforkjoin (middle, end); + right.fork (); A the //Merge and return + returnLeft.join () +Right.join (); - } $ } $ -}
1 /**2 * Cumulative number of implementations3 */4 @Test5 Public voidtest1 () {6 //Start Time7Instant start =Instant.now ();8 9 //A thread pool support is needed hereTenForkjoinpool pool =NewForkjoinpool (); One Aforkjointask<long> task =NewForkjoin (0, 10000000000L); - - Longsum =Pool.invoke (Task); the - //End Time -Instant end =Instant.now (); - System.out.println (Duration.between (Start, end). getseconds ()); +}
Second, java8 parallel stream
The parallel flow is optimized in java8, which simplifies a lot, and we continue to write an example with an accumulation.
1 /**2 * Java8 Parallel Stream parallel ()3 */4 @Test5 Public voidtest2 () {6 //Start Time7Instant start =Instant.now ();8 9longstream.rangeclosed (0, 10000000000L). Parallel ()Ten. Reduce (0, Long:: sum); One A //End Time -Instant end =Instant.now (); - System.out.println (Duration.between (Start, end). getseconds ()); the}
The Java8 not only optimizes the code, but also improves efficiency.
Java8 new features-parallel flow and sequential flow