1
1
package concurrency ;
2
2
3
+ import java .util .Random ;
4
+ import java .util .concurrent .BrokenBarrierException ;
5
+ import java .util .concurrent .CyclicBarrier ;
6
+ import java .util .concurrent .ExecutorService ;
7
+ import java .util .concurrent .Executors ;
8
+ import java .util .concurrent .RecursiveAction ;
9
+ import java .util .concurrent .RecursiveTask ;
10
+
3
11
/**
4
12
*
5
- * @author chengfeili
13
+ * @author chengfeili
6
14
* Jun 24, 2017 4:00:17 PM
7
15
*
8
16
*/
9
17
public class ManagingConcurrentProcesses {
10
18
19
+ /**
20
+ * The CyclicBarrier takes in its constructors a limit value, indicating the
21
+ * number of threads to wait for. As each thread nishes, it calls the
22
+ * await() method on the cyclic barrier. Once the speci ed number of threads
23
+ * have each called await(), the barrier is released and all threads can
24
+ * continue.
25
+ */
26
+ public void creatingACyclicBarrier () {
27
+
28
+ }
29
+
30
+ /**
31
+ * When a task gets too complicated, we can split the task into multiple
32
+ * other tasks using the fork/join framework.
33
+ *
34
+ * The fork/join framework relies on the concept of recursion to solve
35
+ * complex tasks.
36
+ *
37
+ * Applying the fork/join framework requires us to perform three steps:
38
+ * Create a ForkJoinTask. Create the ForkJoinPool. Start the ForkJoinTask.
39
+ *
40
+ */
41
+ public void applyingTheForkJoinFramework () {
42
+
43
+ }
44
+
45
+ public void workingWithARecursiveTask () {
46
+
47
+ }
48
+ }
49
+
50
+ class LionPenManager {
51
+ private void removeAnimals () {
52
+ System .out .println ("Removing animals" );
53
+ }
54
+
55
+ private void cleanPen () {
56
+ System .out .println ("Cleaning the pen" );
57
+ }
58
+
59
+ private void addAnimals () {
60
+ System .out .println ("Adding animals" );
61
+ }
62
+
63
+ public void performTask (CyclicBarrier c1 , CyclicBarrier c2 ) {
64
+ try {
65
+ removeAnimals ();
66
+ c1 .await ();
67
+ cleanPen ();
68
+ c2 .await ();
69
+ addAnimals ();
70
+ } catch (InterruptedException | BrokenBarrierException e ) {
71
+ // Handle checked exceptions here }
72
+ }
73
+ }
74
+
75
+ public void test () {
76
+ ExecutorService service = null ;
77
+ try {
78
+ service = Executors .newFixedThreadPool (4 );
79
+ LionPenManager manager = new LionPenManager ();
80
+ CyclicBarrier c1 = new CyclicBarrier (4 );
81
+ CyclicBarrier c2 = new CyclicBarrier (4 , () -> System .out .println ("*** Pen Cleaned!" ));
82
+ for (int i = 0 ; i < 4 ; i ++)
83
+ service .submit (() -> manager .performTask (c1 , c2 ));
84
+ } finally {
85
+ if (service != null )
86
+ service .shutdown ();
87
+ }
88
+ }
11
89
}
90
+
91
+ /**
92
+ * Removing animals Removing animals Removing animals Removing animals Cleaning
93
+ * the pen Cleaning the pen Cleaning the pen Cleaning the pen *** Pen Cleaned!
94
+ * Adding animals Adding animals Adding animals Adding animals
95
+ */
96
+
97
+ class WeighAnimalAction extends RecursiveAction {
98
+ private int start ;
99
+ private int end ;
100
+ private Double [] weights ;
101
+
102
+ public WeighAnimalAction (Double [] weights , int start , int end ) {
103
+ this .start = start ;
104
+ this .end = end ;
105
+ this .weights = weights ;
106
+ }
107
+
108
+ protected void compute () {
109
+ if (end - start <= 3 )
110
+ for (int i = start ; i < end ; i ++) {
111
+ weights [i ] = (double ) new Random ().nextInt (100 );
112
+ System .out .println ("Animal Weighed: " + i );
113
+ }
114
+ else {
115
+ int middle = start + ((end - start ) / 2 );
116
+ System .out .println ("[start=" + start + ",middle=" + middle + ",end=" + end + "]" );
117
+ invokeAll (new WeighAnimalAction (weights , start , middle ), new WeighAnimalAction (weights , middle , end ));
118
+ }
119
+ }
120
+ }
121
+
122
+ class WeighAnimalTask extends RecursiveTask <Double > {
123
+ private int start ;
124
+ private int end ;
125
+ private Double [] weights ;
126
+
127
+ public WeighAnimalTask (Double [] weights , int start , int end ) {
128
+ this .start = start ;
129
+ this .end = end ;
130
+ this .weights = weights ;
131
+ }
132
+
133
+ protected Double compute () {
134
+ if (end - start <= 3 ) {
135
+ double sum = 0 ;
136
+ for (int i = start ; i < end ; i ++) {
137
+ weights [i ] = (double ) new Random ().nextInt (100 );
138
+ System .out .println ("Animal Weighed: " + i );
139
+ sum += weights [i ];
140
+ }
141
+ return sum ;
142
+ } else {
143
+ int middle = start + ((end - start ) / 2 );
144
+ System .out .println ("[start=" + start + ",middle=" + middle + ",end=" + end + "]" );
145
+ RecursiveTask <Double > otherTask = new WeighAnimalTask (weights , start , middle );
146
+ otherTask .fork ();
147
+ return new WeighAnimalTask (weights , middle , end ).compute () + otherTask .join ();
148
+ }
149
+ }
150
+ }
0 commit comments