In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the new functions of java8". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the new features of java8"?
Parallel flow
Recognize the opening of parallel flow
What is parallel flow? It divides the content of a stream into multiple data blocks and processes the streams of each different data block with different threads. For example, there is an example where the List data needs to be calculated separately in List, and the code is as follows:
List appleList = new ArrayList (); / / pretend that the data is for (Apple apple: appleList) {apple.setPrice (5.0 * apple.getWeight () / 1000);}
In this case, the time complexity is O (list.size), and the time consumption increases with the increase of list. Parallel flow can solve this problem, and the code is as follows:
AppleList.parallelStream () .forEach (apple-> apple.setPrice (5.0 * apple.getWeight () / 1000))
This indicates that the current stream is a parallel stream by calling parallelStream (), and then executes in parallel. The default ForkJoinPool thread pool is used inside the parallel flow, and the default number of threads is the number of cores of the processor.
Test parallel flow
The normal code is as follows:
Public static void main (String [] args) throws InterruptedException {List appleList = initAppleList (); Date begin = new Date (); for (Apple apple: appleList) {apple.setPrice (5.0 * apple.getWeight () / 1000); Thread.sleep (1000);} Date end = new Date () Log.info ("number of apples: {}, time: {} s", appleList.size (), (end.getTime ()-begin.getTime ()) / 1000);}
The output takes 4s.
The parallel code is as follows:
List appleList = initAppleList (); Date begin = new Date (); appleList.parallelStream () .forEach (apple-> {apple.setPrice (5.0 * apple.getWeight () / 1000) Try {Thread.sleep (1000);} catch (InterruptedException e) {e.printStackTrace () }}); Date end = new Date (); log.info ("number of apples: {}, time: {} s", appleList.size (), (end.getTime ()-begin.getTime ()) / 1000)
The output time is 1s. You can see that the time-consuming has been greatly increased by 3s.
Parallel flow splitting will affect the speed of the flow
The following points need to be noted for parallel flows:
For the first n digits processed by the iterate method, it is always slower than the loop, whether parallel or not
For the LongStream.rangeClosed () method, there is no second pain point for iterate. It generates a basic type of value without unpacking operation. In addition, it can directly split the generated number 1-n into four parts: 1-n big 4, 1n/4-2n/4,... 3n/4-n. Therefore, rangeClosed () in parallel is faster than the external iteration of the for loop.
The code is as follows:
Package lambdasinaction.chap7; import java.util.stream.*; public class ParallelStreams {public static long iterativeSum (long n) {long result = 0; for (long I = 0; I + 1) .limit (n) .reduce (Long::sum). Get () } public static long parallelSum (long n) {return Stream.iterate (1L, I-> I + 1) .limit (n). Parallel (). Reduce (Long::sum). Get ();} public static long rangedSum (long n) {return LongStream.rangeClosed (1, n). Reduce (Long::sum). GetAsLong () } public static long parallelRangedSum (long n) {return LongStream.rangeClosed (1, n) .parallel () .reduce (Long::sum) .getAsLong ();}} package lambdasinaction.chap7; import java.util.concurrent.*; import java.util.function.*; public class ParallelStreamsHarness {public static final ForkJoinPool FORK_JOIN_POOL = new ForkJoinPool () Public static void main (String [] args) {System.out.println ("Iterative Sum done in:" + measurePerf (ParallelStreams::iterativeSum, 10000000L) + "msecs"); System.out.println ("Sequential Sum done in:" + measurePerf (ParallelStreams::sequentialSum, 10000000L) + "msecs"); System.out.println ("Parallel forkJoinSum done in:" + measurePerf (ParallelStreams::parallelSum, 10000000L) + "msecs") System.out.println ("Range forkJoinSum done in:" + measurePerf (ParallelStreams::rangedSum, 10000000L) + "msecs"); System.out.println ("Parallel range forkJoinSum done in:" + measurePerf (ParallelStreams::parallelRangedSum, 10000000L) + "msecs");} public static long measurePerf (Function f, T input) {long fastest = Long.MAX_VALUE; for (int I = 0; I < 10) ITunes +) {long start = System.nanoTime (); R result = f.apply (input); long duration = (System.nanoTime ()-start) / 1000000000; System.out.println ("Result:" + result); if (duration < fastest) fastest = duration;} return fastest;}}
Sharing variables can cause data problems
Public static long sideEffectSum (long n) {Accumulator accumulator = new Accumulator (); LongStream.rangeClosed (1, n) .forEach (accumulator::add); return accumulator.total;} public static long sideEffectParallelSum (long n) {Accumulator accumulator = new Accumulator (); LongStream.rangeClosed (1, n). Parallel (). ForEach (accumulator::add); return accumulator.total;} public static class Accumulator {private long total = 0 Public void add (long value) {total + = value;}}
Attention to parallel flow
Try to use raw data streams such as LongStream / IntStream / DoubleStream instead of Stream to process numbers, so as to avoid the extra overhead caused by frequent unpacking
To consider the total computational cost of the flow's operation pipeline, assume that N is the total number of tasks to operate and Q is the time of each operation. N * Q is the total time of the operation, and the higher the Q value, the more likely it is to use parallel flows to bring benefits.
Parallel flow is not recommended for a small amount of data
Stream data that is easy to split into blocks, it is recommended to use parallel streams
Thank you for your reading, the above is the content of "what are the new features of java8". After the study of this article, I believe you have a deeper understanding of the new features of java8, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.