Java8 Secondary School concurrency
This article translated from:http://jaxenter.com/lean-concurrency-in-java-8-49924.html
Reprint Please specify source: http://blog.csdn.net/kingviker/article/details/27057473
Someone has said it before (unfortunately, we have no exact words):
0 Basic program apes find concurrency very difficult.
Intermediate program Apes think concurrency is very easy.
Advanced Program apes find concurrency very difficult.
That's quite right.
On the good side, however, writing concurrent code is easier with lambda expressions and a lot of improved APIs. Java8 concurrency development can at least be improved. Let's take a closer look (Java8 improvements):
JAVA8 improvements to the JDK 1.0 API.
Java.lang.Thread already exists in the JDK 1.0 version number.
Java.lang.Runnable, which is annotated as a functional interface in Java8, is also.
From now on. Almost without the brain we were able to submit runnables to a thread. Let us if we have a very time-consuming operation:
public static int longOperation() { System.out.println("Running on thread #" + Thread.currentThread().getId()); // [...] return 42;}
We are able to pass this operation to the thread in a variety of ways, such as:
thread[] threads = {//Pass a lambda to a thread new thread ((), {Longoperati On (); }),//Pass a method reference to a thread new thread (threadgoodies::longoperation)};//Start all Threadsarrays.stre AM (Threads). foreach (Thread::start);//Join all Threadsarrays.stream (Threads). foreach (T, {try {t.join ();} catch (Interruptedexception ignore) {}});
As we mentioned in our previous blog post, it is a pity that lambda expressions do not have a succinct way of handling the checked exception. None of the new functional interfaces in the Java.util.function package involves throwing a checked exception. Leave this job to the caller.
In the previous blog post, we have published the jooλ (also JOOL,JOO-LAMBDA) package, which wraps each functional interface in the JDK with the same functionality and agrees to throw a checked exception. This is especially useful when using the old JDK API, such as JDBC, or the thread API mentioned above. Using jooλ, we can write this:
// Join all threadsArrays.stream(threads).forEach(Unchecked.consumer( t -> t.join()));
Improved Java5 API in Java8
The multithreading of Java has been very quiet before Java5 's very good Executorservice was announced. Managing multithreading is a burden, and people need additional libraries or a J2ee/jee container to manage the thread pool. These are easy to handle with JAVA5. We are now able to submit a Runnable object or a callable object to Excutorservice, which manages its own thread pool.
Here's a sample of how we can use these Java5 concurrency APIs in Java8:
ExecutorService service = Executors .newFixedThreadPool(5);Future[] answers = { service.submit(() -> longOperation()), service.submit(ThreadGoodies::longOperation)};Arrays.stream(answers).forEach(Unchecked.consumer( f -> System.out.println(f.get())));
Look at that. How do we use the Uncheckedconsumer in jooλ again to wrap the checked exception thrown by calling the Get () method at execution time.
Parallelism and Forkjoinpool in Java8
Today, JAVA8 's streams API has been greatly improved in terms of concurrency and parallelism. In Java8 you can write code such as the following:
Arrays.stream(new int[]{ 1, 2, 3, 4, 5, 6 }) .parallel() .max() .ifPresent(System.out::println);
Although not very necessary in this particular example. But it is interesting to see that only parallel () is called and Intstream.max () is executed to enable Forkjoinpool and you don't have to worry about including Forkjointasks. This is very practical, since not everyone can accept the introduction of the JDK7 Jorkjoin API.
Java8 High School concurrency