Java中的线程并行编程 [英] Parallel programming with threads in Java

查看:121
本文介绍了Java中的线程并行编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有在Java 7中,才可以在Java中进行并行编程. Join/Fork framework出现.

Parallel programming was possible in Java only from Java 7 with the advent of Join/Fork framework.

使用Java在ExecutorService中创建一个线程池,该线程池包含4个线程,并提交给它说10个任务,这意味着4个线程将开始执行4个任务,而其他6个任务将由as和当任何线程完成其任务时.

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.

让我们假设我有一个具有4个内核的Quad处理器,我知道一个线程可以在单个内核上运行(这里不采用超线程概念),所以我所有的4个线程都可以并行工作,一个线程在1个内核上运行?这不是并行编程吗?

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? Isn't it a parallel programming?

编辑:阅读来源-这是Java 8播放列表,在第一章中提到从Java 7开始可以进行并行编程.

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.

推荐答案

对您有误解.

我了解到,随着Join/Fork框架的出现,只有Java 7才可以在Java中进行并行编程.

I read that parallel programming was possible in Java only from Java 7 with the advent of Join/Fork framework.

那是错误的. Join/Fork语句只是另一个抽象层,与裸机"线程相比,该层为您提供了更强大的概念供您使用.

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.

这不是并行编程吗?

Isn't it a parallel programming?

您已经清楚地概述了您的任务将进入一个支持4个线程的池;并且您的硬件也应该支持4个线程.因此,这项工作将并行进行.并且请不要: Fork/Join ExecutorService 相同.相反,两者都是高级概念.旨在使您更轻松地进行并行编程".

You have outlined clearly that your tasks will go into a pool that supports 4 threads; and that your hardware should support 4 threads as well. So that work will happen in parallel. And please not: Fork/Join is not the same as ExecutorService. Instead, both are advanced concepts; intended to make "parallel programming" easier for you.

简要收听了问题中链接的视频后:本教程介绍了Java8 添加流以及在并行流之上的事实(使用下面的Fork/Join框架-是Java 7引入的.

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).

无论如何,该视频都强调了与Java的早期版本相比,并行程序变得很多 更简单.因此,这根本不是要介绍以前无法实现的事情,而是要提供新的 more 功能强大的抽象,使它们更容易地执行这些操作.

In any case, that video emphasizes that parallel program gets much much simpler compared to earlier versions of 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.

这篇关于Java中的线程并行编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆