Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132930 views
License: OTHER
1
import java.util.ArrayList;
2
import java.util.List;
3
import java.util.concurrent.atomic.AtomicLong;
4
5
public class Main {
6
public static final int BIG_NR = 2000000;
7
public static AtomicLong bigSum = new AtomicLong();
8
9
public static void main(String[] args) {
10
List<Thread> threads = new ArrayList<Thread>();
11
for (int i = 0; i < 50; i++) {
12
Runnable task = new Sum(BIG_NR);
13
Thread worker = new Thread(task);
14
worker.start();
15
threads.add(worker);
16
}
17
18
int running = 0;
19
do {
20
running = 0;
21
for (Thread thread : threads) {
22
if (thread.isAlive()) {
23
running++;
24
}
25
}
26
System.out.println("Remaining threads: " + running);
27
} while (running > 0);
28
29
System.out.println(Main.bigSum);
30
31
}
32
}
33
34