Parallel programming with threads in Java

Let's say that in Java, using executorservice, I create a 4-thread thread pool and submit it to say 10 tasks, which means that 4 threads will start to execute 4 tasks, and the other 6 tasks will be obtained by threads. When any thread completes its tasks

Suppose I have a four core processor with four cores. I know that a thread can run on one kernel (the concept of hyper threading is not adopted here). Therefore, all four threads work in parallel, and one kernel runs one thread? Isn't this a parallel programming?

Edit: source of reading – this is a Java 8 playlist. In Chapter 1, it is mentioned that parallel programming can be started from Java 7

Solution

There is a misunderstanding about your ending

That's wrong The join / fork statement is just another abstraction layer that provides you with a more powerful concept - compared to "bare metal" threads

You have clearly outlined that your task will enter a pool that supports 4 threads; And your hardware should also support 4 threads Such work will be carried out at the same time Please do not: fork / join is different from executorservice On the contrary, both are advanced concepts; Designed to make it easier for you to "program in parallel."

After briefly listening back to the video linked in the question: this tutorial is about the fact that Java 8 adds streams and is on top of parallel streams (using the fork / join framework below – which was introduced in Java 7)

In any case, the video emphasizes that parallel programs are simpler than earlier versions of Java So it's not about introducing previously impossible things at all - it's about providing new and more powerful abstractions that make it easier to do these things

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>