forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupervisorWorkerEx.java
More file actions
233 lines (168 loc) · 5.48 KB
/
SupervisorWorkerEx.java
File metadata and controls
233 lines (168 loc) · 5.48 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package com.zetcode;
import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingWorker;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static javax.swing.GroupLayout.Alignment.CENTER;
/*
* This program initiates a background search for
* various image files in a user's home directory.
*
*/
class MyLabel extends JLabel {
public MyLabel(String text) {
super(text);
setPreferredSize(new Dimension(170, 40));
setMaximumSize(new Dimension(Short.MAX_VALUE,
Short.MAX_VALUE));
setHorizontalAlignment(CENTER);
setBackground(Color.lightGray);
}
@Override
public boolean isOpaque() {
return true;
}
}
public class SupervisorWorkerEx extends JFrame {
private final int NUMBER_OF_LABELS = 6;
private ArrayList<MyLabel> labels;
private JButton btn;
public SupervisorWorkerEx() {
initUI();
}
private void initUI() {
labels = new ArrayList<>(NUMBER_OF_LABELS);
for (int i = 0; i < NUMBER_OF_LABELS; i++) {
labels.add(new MyLabel("0"));
}
btn = new JButton("Start");
btn.addActionListener(new StartAction());
createLayout();
setTitle("Supervisor worker");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void createLayout() {
var pane = getContentPane();
var gl = new GroupLayout(pane);
pane.setLayout(gl);
gl.setAutoCreateContainerGaps(true);
gl.setHorizontalGroup(gl.createParallelGroup(CENTER)
.addComponent(labels.get(0))
.addComponent(labels.get(1))
.addComponent(labels.get(2))
.addComponent(labels.get(3))
.addComponent(labels.get(4))
.addComponent(labels.get(5))
.addComponent(btn)
);
gl.setVerticalGroup(gl.createSequentialGroup()
.addComponent(labels.get(0))
.addGap(3)
.addComponent(labels.get(1))
.addGap(3)
.addComponent(labels.get(2))
.addGap(3)
.addComponent(labels.get(3))
.addGap(3)
.addComponent(labels.get(4))
.addGap(3)
.addComponent(labels.get(5))
.addGap(10)
.addComponent(btn)
);
pack();
}
private class StartAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
var cmd = e.getActionCommand();
if (null != cmd) {
switch (cmd) {
case "Restore" -> doRestore();
case "Start" -> doStartWorkers();
}
}
}
private void doRestore() {
btn.setText("Start");
for (JLabel label : labels) {
label.setText("0");
label.setBackground(Color.lightGray);
}
}
private void doStartWorkers() {
btn.setEnabled(false);
var latch = new CountDownLatch(NUMBER_OF_LABELS);
ExecutorService executor
= Executors.newFixedThreadPool(NUMBER_OF_LABELS);
for (JLabel label : labels) {
label.setBackground(Color.white);
executor.execute(new Counter(label, latch));
}
var supervisor = new Supervisor(latch);
supervisor.execute();
}
}
private class Supervisor extends SwingWorker<Void, Void> {
private final CountDownLatch latch;
public Supervisor(CountDownLatch latch) {
this.latch = latch;
}
@Override
protected Void doInBackground() throws InterruptedException {
latch.await();
return null;
}
@Override
protected void done() {
btn.setText("Restore");
btn.setEnabled(true);
}
}
private static class Counter extends SwingWorker<Void, Integer> {
private final JLabel label;
private final CountDownLatch latch;
public Counter(JLabel label, CountDownLatch latch) {
this.label = label;
this.latch = latch;
}
@Override
protected Void doInBackground() throws InterruptedException {
var rand = new Random();
int delay = rand.nextInt(50) + 10;
for (int i = 1; i <= 100; i++) {
publish(i);
Thread.sleep(delay);
}
return null;
}
@Override
protected void process(List<Integer> values) {
label.setText(values.get(values.size() - 1).toString());
}
@Override
protected void done() {
label.setBackground(Color.green);
latch.countDown();
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
var ex = new SupervisorWorkerEx();
ex.setVisible(true);
});
}
}