简体   繁体   English

在Java中使用线程进行并行编程

[英]Parallel programming with threads in Java

Parallel programming was possible in Java only from Java 7 with the advent of Join/Fork framework . 随着Join/Fork framework的出现,Java中只能从Java 7进行并行编程。

Let's say in Java, using ExecutorService I create a thread pool of say 4 threads and submit to it say 10 tasks which means 4 threads will start executing the 4 tasks and other 6 tasks will be picked up by threads as and when any thread finishes its task. 让我们说在Java中,使用ExecutorService我创建一个4线程的线程池并提交给它说10个任务,这意味着4个线程将开始执行4个任务,其他6个任务将由线程获取,当任何线程完成其任务。

Let's assume I have a quad processor having 4 cores, I know a thread can be run on a single core(not taking hyper-threading concept here) so will all my 4 threads work in parallel, 1 thread running on 1 core? 假设我有一个具有4个内核的四核处理器,我知道一个线程可以在一个内核上运行(这里没有采用超线程概念)所以我的所有4个线程并行工作,1个内核运行1个线程? Isn't it a parallel programming? 这不是一个并行编程吗?

EDIT: Source of reading - This is the Java 8 playlist where in chapter 1 it was referred that parallel programming was possible from Java 7 onwards. 编辑: 阅读源 - 这是Java 8播放列表,在第1章中提到可以从Java 7开始实现并行编程。

There is a misconception on your end. 你的结局存在误解。

I read that parallel programming was possible in Java only from Java 7 with the advent of Join/Fork framework. 我读到,随着Join / Fork框架的出现,只能从Java 7开始在Java中实现并行编程。

That is wrong. 那是错的。 The Join/Fork statement is simply yet another abstraction layer that gives you more powerful concepts to work with - compared to "bare metal" threads. Join / Fork语句只是另一个抽象层,它为您提供了更强大的概念 - 与“裸机”线程相比。

Isn't it a parallel programming? 这不是一个并行编程吗?

You have outlined clearly that your tasks will go into a pool that supports 4 threads; 您已清楚地概述了您的任务将进入支持4个线程的池中; and that your hardware should support 4 threads as well. 并且您的硬件也应该支持4个线程。 So that work will happen in parallel. 这样的工作将同时进行。 And please not: Fork/Join is not the same as ExecutorService . 而请勿: 叉/加入一样的ExecutorService。 Instead, both are advanced concepts; 相反,两者都是先进的概念; intended to make "parallel programming" easier for you. 旨在使您更容易“并行编程”。

After briefly listening into the video linked in the question: the tutorial is about the fact that Java8 added streams, and on top of that parallel streams (which use the Fork/Join framework underneath - that one was introduced with Java 7). 在简要回听了问题中链接的视频之后:该教程是关于Java8 添加了流的事实,并且在并行流之上(使用下面的Fork / Join框架 - 这是Java 7引入的)。

In any case, that video emphasizes that parallel program gets much much simpler compared to earlier versions of Java. 无论如何,该视频强调与早期版本的Java相比,并行程序变得更加 简单 So it is not at all about introducing something that wasn't possible before - but about providing new more powerful abstractions that make it easier to do such things. 所以它根本不是介绍以前不可能的东西 - 而是提供新的强大的抽象,使得更容易做这些事情。

Parallel programming was possible in Java only from Java 7 with the advent of Join/Fork framework. 随着Join / Fork框架的出现,Java中只能从Java 7进行并行编程。

Parallel programming exists in java since early versions. 自早期版本以来,Java中存在并行编程。 It was enhanced with Java 5 java.util.concurrent package classes and Java 7 ForkJoinPool further enhanced parallel programming to new level. 它使用Java 5 java.util.concurrent包类进行了增强,Java 7 ForkJoinPool进一步增强了并行编程到新的水平。

You can find answer to your questions in this article by oracle. 您可以通过oracle在本文中找到您的问题的答案。

Java Platform, Standard Edition (Java SE) 5 and then Java SE 6 introduced a set of packages providing powerful concurrency building blocks. Java平台,标准版(Java SE)5和Java SE 6引入了一组包,提供了强大的并发构建块。 Java SE 7 further enhanced them by adding support for parallelism Java SE 7通过添加对并行性的支持进一步增强了它们

Example of divide and conquer problem: 分而治之问题的例子:

Find sum of integers in large array 查找大数组中的整数之和

Instead of sequentially computing the sum, divide array into multiple partitions and assign computation task on each partition to different task. 不是顺序计算总和,而是将数组划分为多个分区,并将每个分区上的计算任务分配给不同的任务。

在此输入图像描述

The problem with the executors for implementing divide and conquer algorithms is not related to creating subtasks, because a Callable is free to submit a new subtask to its executor and wait for its result in a synchronous or asynchronous fashion . 实现分而治之算法的执行程序的问题与创建子任务无关,因为Callable可以自由地向其执行程序提交新的子任务并以同步或异步方式等待其结果

The issue is that of parallelism: When a Callable waits for the result of another Callable , it is put in a waiting state, thus wasting an opportunity to handle another Callable queued for execution. 问题在于并行性:Callable等待另一个Callable的结果时,它处于等待状态,因此浪费了处理排队等待执行的另一个Callable的机会。

The fork/join framework added to the java.util.concurrent package in Java SE 7 fork / join框架添加到Java SE 7中java.util.concurrent包中

Additions for Supporting Parallelism: 支持并行的附加功能:

The core addition is a new ForkJoinPool executor that is dedicated to running instances implementing ForkJoinTask. 核心添加是一个新的ForkJoinPool执行器,专门用于运行实现ForkJoinTask的实例。 ForkJoinTask objects support the creation of subtasks plus waiting for the subtasks to complete. ForkJoinTask对象支持创建子任务以及等待子任务完成。 With those clear semantics, the executor is able to dispatch tasks among its internal threads pool by “ stealing ” jobs when a task is waiting for another task to complete and there are pending tasks to be run. 使用这些明确的语义,执行程序能够在任务等待另一个任务完成并且有待执行的任务运行时通过“ 窃取 ”作业在其内部线程池之间分派任务。

ForkJoinTask objects feature two specific methods: ForkJoinTask对象具有两种特定方法:

The fork() method allows a ForkJoinTask to be planned for asynchronous execution. fork()方法允许计划ForkJoinTask进行异步执行。 This allows a new ForkJoinTask to be launched from an existing one. 这允许从现有的ForkJoinTask启动新的ForkJoinTask

In turn, the join() method allows a ForkJoinTask to wait for the completion of another one. 反过来, join()方法允许ForkJoinTask等待另一个完成。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM