-
Notifications
You must be signed in to change notification settings - Fork 11
/
ManagingConcurrentProcesses.java
150 lines (130 loc) · 3.93 KB
/
ManagingConcurrentProcesses.java
1
2
3
4
5
6
7
8
9
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package concurrency;
import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RecursiveAction;
import java.util.concurrent.RecursiveTask;
/**
*
* @author chengfeili
* Jun 24, 2017 4:00:17 PM
*
*/
public class ManagingConcurrentProcesses {
/**
* The CyclicBarrier takes in its constructors a limit value, indicating the
* number of threads to wait for. As each thread nishes, it calls the
* await() method on the cyclic barrier. Once the specified number of threads
* have each called await(), the barrier is released and all threads can
* continue.
*/
public void creatingACyclicBarrier() {
}
/**
* When a task gets too complicated, we can split the task into multiple
* other tasks using the fork/join framework.
*
* The fork/join framework relies on the concept of recursion to solve
* complex tasks.
*
* Applying the fork/join framework requires us to perform three steps:
* Create a ForkJoinTask. Create the ForkJoinPool. Start the ForkJoinTask.
*
*/
public void applyingTheForkJoinFramework() {
}
public void workingWithARecursiveTask() {
}
}
class LionPenManager {
private void removeAnimals() {
System.out.println("Removing animals");
}
private void cleanPen() {
System.out.println("Cleaning the pen");
}
private void addAnimals() {
System.out.println("Adding animals");
}
public void performTask(CyclicBarrier c1, CyclicBarrier c2) {
try {
removeAnimals();
c1.await();
cleanPen();
c2.await();
addAnimals();
} catch (InterruptedException | BrokenBarrierException e) {
// Handle checked exceptions here }
}
}
public void test() {
ExecutorService service = null;
try {
service = Executors.newFixedThreadPool(4);
LionPenManager manager = new LionPenManager();
CyclicBarrier c1 = new CyclicBarrier(4);
CyclicBarrier c2 = new CyclicBarrier(4, () -> System.out.println("*** Pen Cleaned!"));
for (int i = 0; i < 4; i++)
service.submit(() -> manager.performTask(c1, c2));
} finally {
if (service != null)
service.shutdown();
}
}
}
/**
* Removing animals Removing animals Removing animals Removing animals Cleaning
* the pen Cleaning the pen Cleaning the pen Cleaning the pen *** Pen Cleaned!
* Adding animals Adding animals Adding animals Adding animals
*/
class WeighAnimalAction extends RecursiveAction {
private int start;
private int end;
private Double[] weights;
public WeighAnimalAction(Double[] weights, int start, int end) {
this.start = start;
this.end = end;
this.weights = weights;
}
protected void compute() {
if (end - start <= 3)
for (int i = start; i < end; i++) {
weights[i] = (double) new Random().nextInt(100);
System.out.println("Animal Weighed: " + i);
}
else {
int middle = start + ((end - start) / 2);
System.out.println("[start=" + start + ",middle=" + middle + ",end=" + end + "]");
invokeAll(new WeighAnimalAction(weights, start, middle), new WeighAnimalAction(weights, middle, end));
}
}
}
class WeighAnimalTask extends RecursiveTask<Double> {
private int start;
private int end;
private Double[] weights;
public WeighAnimalTask(Double[] weights, int start, int end) {
this.start = start;
this.end = end;
this.weights = weights;
}
protected Double compute() {
if (end - start <= 3) {
double sum = 0;
for (int i = start; i < end; i++) {
weights[i] = (double) new Random().nextInt(100);
System.out.println("Animal Weighed: " + i);
sum += weights[i];
}
return sum;
} else {
int middle = start + ((end - start) / 2);
System.out.println("[start=" + start + ",middle=" + middle + ",end=" + end + "]");
RecursiveTask<Double> otherTask = new WeighAnimalTask(weights, start, middle);
otherTask.fork();
return new WeighAnimalTask(weights, middle, end).compute() + otherTask.join();
}
}
}