在Java7中,JDK提供对多线程开发提供了一个非常强大的框架,就是Fork/Join框架。这个是对原来的Executors更
进一步,在原来的基础上增加了并行分治计算中的一种Work-stealing策略,就是指的是。当一个线程正在等待他创建的
子线程运行的时候,当前线程如果完成了自己的任务后,就会寻找还没有被运行的任务并且运行他们,这样就是和
Executors这个方式最大的区别,更加有效的使用了线程的资源和功能。所以非常推荐使用Fork/Join框架。
下面我们以一个例子来说明这个框架如何使用,主要就是创建一个含有10000个资源的List,分别去修改他的内容。
In this example, you have created a ForkJoinPool object and a subclass of the
ForkJoinTask class that you execute in the pool. To create the ForkJoinPool object,
you have used the constructor without arguments, so it will be executed with its default
configuration. It creates a pool with a number of threads equal to the number of processors
of the computer. When the ForkJoinPool object is created, those threads are created and
they wait in the pool until some tasks arrive for their execution.
Since the Task class doesn't return a result, it extends the RecursiveAction class. In the
recipe, you have used the recommended structure for the implementation of the task. If the
task has to update more than 10 products, it divides those set of elements into two blocks,
creates two tasks, and assigns a block to each task. You have used the first and last
attributes in the Task class to know the range of positions that this task has to update in the
list of products. You have used the first and last attributes to use only one copy of the
products list and not create different lists for each task.
To execute the subtasks that a task creates, it calls the invokeAll() method. This is a
synchronous call, and the task waits for the finalization of the subtasks before continuing
(potentially finishing) its execution. While the task is waiting for its subtasks, the worker thread
that was executing it takes another task that was waiting for execution and executes it. With
this behavior, the Fork/Join framework offers a more efficient task management than the
Runnable and Callable objects themselves.
between the Executor and the Fork/Join framework. In the Executor framework, all the tasks
have to be sent to the executor, while in this case, the tasks include methods to execute and
control the tasks inside the pool. You have used the invokeAll() method in the Task class,
that extends the RecursiveAction class that extends the ForkJoinTask class.
You have sent a unique task to the pool to update all the list of products using the execute()
method. In this case, it's an asynchronous call, and the main thread continues its execution.
You have used some methods of the ForkJoinPool class to check the status and the
evolution of the tasks that are running. The class includes more methods that can be useful
for this purpose. See the Monitoring a Fork/Join pool recipe for a complete list of
those methods.
Finally, like with the Executor framework, you should finish ForkJoinPool using the
shutdown() method.
ava7引入了Fork Join的概念,来更好的支持并行运算。顾名思义,Fork Join类似与流程语言的分支,合并的概念。也就是说Java7 SE原生支持了在一个主线程中开辟多个分支线程,并且根据分支线程的逻辑来等待(或者不等待)汇集,当然你也可以fork的某一个分支线程中再开辟Fork Join,这也就可以实现Fork Join的嵌套。
有两个核心类ForkJoinPool和ForkJoinTask。
ForkJoinPool实现了ExecutorService接口,起到线程池的作用。所以他的用法和Executor框架的使用时一样的,当然Fork Join本身就是Executor框架的扩展。ForkJoinPool有3个关键的方法,来启动线程,execute(...),invoke(...),submit(...)。具体描述如下:
客户端非fork/join调用 | 内部调用fork/join | |
异步执行 | execute(ForkJoinTask) | ForkJoinTask.fork |
等待获取结果 | invoke(ForkJoinTask) | ForkJoinTask.invoke |
执行,获取Futrue | submit(ForkJoinTask) | ForkJoinTask.fork(ForkJoinTasks are Futures) |
ForkJoinTask是分支合并的执行任何,分支合并的业务逻辑使用者可以再继承了这个抽先类之后,在抽象方法exec()中实现。其中exec()的返回结果和ForkJoinPool的执行调用方(execute(...),invoke(...),submit(...)),共同决定着线程是否阻塞,具体请看下面的测试用例。
首先,用户需要创建一个自己的ForkJoinTask。代码如下:
- public class MyForkJoinTask<V> extends ForkJoinTask<V> {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private V value;
- private boolean success = false;
- @Override
- public V getRawResult() {
- return value;
- }
- @Override
- protected void setRawResult(V value) {
- this.value = value;
- }
- @Override
- protected boolean exec() {
- System.out.println("exec");
- return this.success;
- }
- public boolean isSuccess() {
- return success;
- }
- public void setSuccess(boolean isSuccess) {
- this.success = isSuccess;
- }
- }
测试ForkJoinPool.invoke(...):
- @Test
- public void testForkJoinInvoke() throws InterruptedException, ExecutionException {
- ForkJoinPool forkJoinPool = new ForkJoinPool();
- MyForkJoinTask<String> task = new MyForkJoinTask<String>();
- task.setSuccess(true);
- task.setRawResult("test");
- String invokeResult = forkJoinPool.invoke(task);
- assertEquals(invokeResult, "test");
- }
- @Test
- public void testForkJoinInvoke2() throws InterruptedException, ExecutionException {
- final ForkJoinPool forkJoinPool = new ForkJoinPool();
- final MyForkJoinTask<String> task = new MyForkJoinTask<String>();
- new Thread(new Runnable() {
- public void run() {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- }
- task.complete("test");
- }
- }).start();
- // exec()返回值是false,此处阻塞,直到另一个线程调用了task.complete(...)
- String result = forkJoinPool.invoke(task);
- System.out.println(result);
- }
- @Test
- public void testForkJoinSubmit() throws InterruptedException, ExecutionException {
- final ForkJoinPool forkJoinPool = new ForkJoinPool();
- final MyForkJoinTask<String> task = new MyForkJoinTask<String>();
- task.setSuccess(true); // 是否在此任务运行完毕后结束阻塞
- ForkJoinTask<String> result = forkJoinPool.submit(task);
- result.get(); // 如果exec()返回值是false,在此处会阻塞,直到调用complete
- }
测试ForkJoinPool.submit(...):
- @Test
- public void testForkJoinSubmit() throws InterruptedException, ExecutionException {
- final ForkJoinPool forkJoinPool = new ForkJoinPool();
- final MyForkJoinTask<String> task = new MyForkJoinTask<String>();
- task.setSuccess(true); // 是否在此任务运行完毕后结束阻塞
- ForkJoinTask<String> result = forkJoinPool.submit(task);
- result.get(); // 如果exec()返回值是false,在此处会阻塞,直到调用complete
- }
- @Test
- public void testForkJoinSubmit2() throws InterruptedException, ExecutionException {
- final ForkJoinPool forkJoinPool = new ForkJoinPool();
- final MyForkJoinTask<String> task = new MyForkJoinTask<String>();
- forkJoinPool.submit(task);
- Thread.sleep(1000);
- }
- @Test
- public void testForkJoinSubmit3() throws InterruptedException, ExecutionException {
- final ForkJoinPool forkJoinPool = new ForkJoinPool();
- final MyForkJoinTask<String> task = new MyForkJoinTask<String>();
- new Thread(new Runnable() {
- public void run() {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- }
- task.complete("test");
- }
- }).start();
- ForkJoinTask<String> result = forkJoinPool.submit(task);
- // exec()返回值是false,此处阻塞,直到另一个线程调用了task.complete(...)
- result.get();
- Thread.sleep(1000);
- }
测试ForkJoinPool.execute(...):
- @Test
- public void testForkJoinExecute() throws InterruptedException, ExecutionException {
- ForkJoinPool forkJoinPool = new ForkJoinPool();
- MyForkJoinTask<String> task = new MyForkJoinTask<String>();
- forkJoinPool.execute(task); // 异步执行,无视task.exec()返回值。
- }