Статьи

Java 7: пример Fork / Join Framework

Платформа Fork / Join в Java 7 предназначена для работы, которая может быть разбита на более мелкие задачи, а результаты этих задач объединены для получения окончательного результата. В целом, классы, использующие Fork / Join Framework, следуют следующему простому алгоритму:

01
02
03
04
05
06
07
08
09
10
11
12
13
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">// pseudocode</span> // псевдокод</span>
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">Result solve(Problem problem) {</span> Результат решить (проблема проблема) {</span>
  <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">if (problem.size < SEQUENTIAL_THRESHOLD)</span> если (problem.size <SEQUENTIAL_THRESHOLD)</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">return solveSequentially(problem);</span> возвращаемся решить сам по себе (проблема);</span>
  <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">else {</span> еще {</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">Result left, right;</span> Результат слева, справа;</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">INVOKE-IN-PARALLEL {</span> INVOKE-IN-PARALLEL {</span>
      <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">left = solve(extractLeftHalf(problem));</span> left = решить (extractLeftHalf (проблема));</span>
      <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">right = solve(extractRightHalf(problem));</span> право = решить (extractRightHalf (проблема));</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">return combine(left, right);</span> возвратный комбайн (слева, справа);</span>
  <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>

Чтобы продемонстрировать это, я создал пример, чтобы найти максимальное число из большого массива, используя fork / join:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">import java.util.Random;</span> импорт java.util.Random;</span>
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">import java.util.concurrent.ForkJoinPool;</span> import java.util.concurrent.ForkJoinPool;</span>
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">import java.util.concurrent.RecursiveTask;</span> import java.util.concurrent.RecursiveTask;</span>
 
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">public class MaximumFinder extends RecursiveTask<Integer> {</span> открытый класс MaximumFinder extends RecursiveTask <Integer> {</span>
 
  <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">private static final int SEQUENTIAL_THRESHOLD = 5;</span> приватная статическая финал int SEQUENTIAL_THRESHOLD = 5;</span>
 
  <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">private final int[] data;</span> private final int [] data;</span>
  <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">private final int start;</span> закрытый финал int start;</span>
  <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">private final int end;</span> закрытый финал int end;</span>
 
  <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">public MaximumFinder(int[] data, int start, int end) {</span> public MaximumFinder (int [] data, int start, int end) {</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">this.data = data;</span> this.data = data;</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">this.start = start;</span> this.start = начало;</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">this.end = end;</span> this.end = end;</span>
  <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
 
  <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">public MaximumFinder(int[] data) {</span> public MaximumFinder (int [] data) {</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">this(data, 0, data.length);</span> this (data, 0, data.length);</span>
  <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
 
  <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">@Override</span> @Override</span>
  <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">protected Integer compute() {</span> Защищенное целочисленное вычисление () {</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">final int length = end - start;</span> final int length = end - начало;</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">if (length < SEQUENTIAL_THRESHOLD) {</span> if (длина <SEQUENTIAL_THRESHOLD) {</span>
      <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">return computeDirectly();</span> return computeDirectly ();</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">final int split = length / 2;</span> final int split = длина / 2;</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">final MaximumFinder left = new MaximumFinder(data, start, start + split);</span> final MaximumFinder left = new MaximumFinder (данные, старт, старт + разделение);</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">left.fork();</span> left.fork ();</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">final MaximumFinder right = new MaximumFinder(data, start + split, end);</span> окончательное MaximumFinder вправо = новый MaximumFinder (данные, начало + разделение, конец);</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">return Math.max(right.compute(), left.join());</span> вернуть Math.max (right.compute (), left.join ());</span>
  <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
 
  <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">private Integer computeDirectly() {</span> private Integer computeDirectly () {</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">System.out.println(Thread.currentThread() + ' computing: ' + start</span> System.out.println (Thread.currentThread () + 'вычисления:' + начало</span>
                       <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">+ ' to ' + end);</span> + 'to' + end);</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">int max = Integer.MIN_VALUE;</span> int max = Integer.MIN_VALUE;</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">for (int i = start; i < end; i++) {</span> for (int i = start; i <end; i ++) {</span>
      <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">if (data[i] > max) {</span> if (data [i]> max) {</span>
        <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">max = data[i];</span> max = данные [i];</span>
      <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">return max;</span> возврат макс;</span>
  <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
 
  <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">public static void main(String[] args) {</span> public static void main (String [] args) {</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">// create a random data set</span> // создаем случайный набор данных</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">final int[] data = new int[1000];</span> final int [] data = new int [1000];</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">final Random random = new Random();</span> окончательный случайный случайный = новый случайный ();</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">for (int i = 0; i < data.length; i++) {</span> for (int i = 0; i <data.length; i ++) {</span>
      <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">data[i] = random.nextInt(100);</span> data [i] = random.nextInt (100);</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
 
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">// submit the task to the pool</span> // отправить задачу в пул</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">final ForkJoinPool pool = new ForkJoinPool(4);</span> окончательный пул ForkJoinPool = новый ForkJoinPool (4);</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">final MaximumFinder finder = new MaximumFinder(data);</span> конечный искатель MaximumFinder = новый MaximumFinder (данные);</span>
    <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">System.out.println(pool.invoke(finder));</span> System.out.println (pool.invoke (искатель));</span>
  <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>

Класс MaximumFinder — это RecursiveTask который отвечает за поиск максимального числа из массива. Если размер массива меньше порогового значения (5), найдите максимальный размер напрямую, перебирая массив. В противном случае, разбейте массив на две половины, наберите рекурсив на каждой половине и дождитесь их завершения ( join ). Получив результат каждой половины, мы можем найти максимум двух и вернуть его.

Ссылка: Java 7: пример Fork / Join Framework от нашего партнера по JCG Фахда Шарифа в блоге fahd.blog .