|
4 | 4 | import java.util.List; |
5 | 5 | import java.util.concurrent.ForkJoinPool; |
6 | 6 | import java.util.concurrent.ForkJoinTask; |
7 | | -import java.util.concurrent.RecursiveAction; |
8 | 7 | import java.util.concurrent.RecursiveTask; |
9 | 8 |
|
10 | 9 | public class ForkJoin_RecursiveTask { |
11 | 10 |
|
12 | 11 | // tag::code[] |
13 | | - // Classe que representa a tarefa que será executada |
14 | | - static class ImpressaoDeStrings extends RecursiveTask<Integer> { |
| 12 | + // Class that represents the task that will be performed |
| 13 | + static class StringPrinter extends RecursiveTask<Integer> { |
15 | 14 |
|
16 | | - private String stringParaImprimir; // String que será impressa |
| 15 | + private String stringToPrint; |
17 | 16 |
|
18 | | - public ImpressaoDeStrings(String stringParaImprimir) { |
19 | | - this.stringParaImprimir = stringParaImprimir; |
| 17 | + public StringPrinter(String stringToPrint) { |
| 18 | + this.stringToPrint = stringToPrint; |
20 | 19 | } |
21 | 20 |
|
22 | 21 | @Override |
23 | 22 | protected Integer compute() { |
24 | | - if (stringParaImprimir.length() < 10) { |
25 | | - // se a String tiver menos de 10 caracteres, será impressa |
26 | | - System.out.println(Thread.currentThread().getName() + " - " + stringParaImprimir); |
27 | | - |
28 | | - // retornamos a quantidade de caracteres impressos |
29 | | - return stringParaImprimir.length(); |
| 23 | + if (stringToPrint.length() < 10) { |
| 24 | + // if String is less than 10 characters, will print |
| 25 | + System.out.println(Thread.currentThread().getName() + " - " + stringToPrint); |
| 26 | + |
| 27 | + // return the amount of characters printed |
| 28 | + return stringToPrint.length(); |
30 | 29 | } else { |
31 | | - // caso contrário, são criadas duas novas tarefas, uma com a primeira metade da String |
32 | | - // e outra com a segunda metade da String |
33 | | - List<ImpressaoDeStrings> novasTarefas = divideEmDuasTarefas(); |
34 | | - ImpressaoDeStrings tarefa1 = novasTarefas.get(0); |
35 | | - ImpressaoDeStrings tarefa2 = novasTarefas.get(1); |
36 | | - |
37 | | - // 'fork' irá enviar a tarefa1 para ser executada em uma nova thread |
38 | | - ForkJoinTask<Integer> primeiraTarefa = tarefa1.fork(); |
39 | | - |
40 | | - // 'compute' irá executar a tarefa2 nessa mesma thread |
41 | | - Integer resultadoDaTarefa2 = tarefa2.compute(); |
42 | | - |
43 | | - // 'join' irá pegar o resultado da tafera1 que estava sendo executada em outra thread |
44 | | - Integer resultadoDaTarefa1 = primeiraTarefa.join(); |
45 | | - |
46 | | - // retornamos a soma dos resultados, pois é a quantidade de carateres impressos |
47 | | - return resultadoDaTarefa2 + resultadoDaTarefa1; |
| 30 | + // otherwise, two new tasks are created, one with the first half of String and one with the second half of String |
| 31 | + List<StringPrinter> newTasks = divideInTwoTasks(); |
| 32 | + StringPrinter task1 = newTasks.get(0); |
| 33 | + StringPrinter task2 = newTasks.get(1); |
| 34 | + |
| 35 | + // 'fork' will send task1 to run on a new thread |
| 36 | + ForkJoinTask<Integer> firstTask = task1.fork(); |
| 37 | + |
| 38 | + // 'compute' will execute task2 on this same thread |
| 39 | + Integer taskResult2 = task2.compute(); |
| 40 | + |
| 41 | + // 'join' will get the result of task1 that was running on another thread |
| 42 | + Integer taskResult1 = firstTask.join(); |
| 43 | + |
| 44 | + // return the sum of the results because it is the number of characters printed |
| 45 | + return taskResult2 + taskResult1; |
48 | 46 | } |
49 | 47 | } |
50 | 48 |
|
51 | | - private List<ImpressaoDeStrings> divideEmDuasTarefas() { |
52 | | - // esse método divide a String em duas partes e cria duas novas tarefas |
53 | | - // cada uma das tarefas recebe uma parte da String |
54 | | - |
55 | | - int tamanhoDaString = stringParaImprimir.length(); |
56 | | - int meioDaString = tamanhoDaString / 2; |
57 | | - |
58 | | - String primeiraMetade = stringParaImprimir.substring(0, meioDaString); |
59 | | - String segundaMetade = stringParaImprimir.substring(meioDaString); |
60 | | - |
61 | | - List<ImpressaoDeStrings> acoes = new ArrayList<ImpressaoDeStrings>(); |
62 | | - acoes.add(new ImpressaoDeStrings(primeiraMetade)); |
63 | | - acoes.add(new ImpressaoDeStrings(segundaMetade)); |
64 | | - return acoes; |
| 49 | + private List<StringPrinter> divideInTwoTasks() { |
| 50 | + // this method splits the string into two parts and creates two new tasks |
| 51 | + // each task gets a part of the String |
| 52 | + |
| 53 | + int stringSize = stringToPrint.length(); |
| 54 | + int stringMiddle = stringSize / 2; |
| 55 | + |
| 56 | + String firstHalf = stringToPrint.substring(0, stringMiddle); |
| 57 | + String secondHalf = stringToPrint.substring(stringMiddle); |
| 58 | + |
| 59 | + List<StringPrinter> actions = new ArrayList<StringPrinter>(); |
| 60 | + actions.add(new StringPrinter(firstHalf)); |
| 61 | + actions.add(new StringPrinter(secondHalf)); |
| 62 | + return actions; |
65 | 63 | } |
66 | 64 |
|
67 | 65 | } |
68 | 66 |
|
69 | 67 | public static void main(String[] args) { |
70 | | - // string que queremos imprimir |
71 | | - String stringParaImprimir = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
72 | | - |
73 | | - // tarefa principal que será executada |
74 | | - ImpressaoDeStrings tarefa = new ImpressaoDeStrings(stringParaImprimir); |
75 | | - |
76 | | - // criação do ForkJoinPool e execução da tarefa |
| 68 | + // string we want to print |
| 69 | + String stringToPrint= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 70 | + |
| 71 | + // main task to be performed |
| 72 | + StringPrinter task = new StringPrinter(stringToPrint); |
| 73 | + |
| 74 | + // ForkJoinPool creation and task execution |
77 | 75 | ForkJoinPool forkJoinPool = new ForkJoinPool(); |
78 | | - Integer resultado = forkJoinPool.invoke(tarefa); |
79 | | - System.out.println("Resultado da execução: " + resultado); |
| 76 | + Integer result = forkJoinPool.invoke(task); |
| 77 | + System.out.println("Execution result: " + result); |
| 78 | + |
80 | 79 | } |
81 | 80 | // end::code[] |
82 | 81 |
|
|
0 commit comments