-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIntPrinter.java
More file actions
32 lines (28 loc) · 938 Bytes
/
IntPrinter.java
File metadata and controls
32 lines (28 loc) · 938 Bytes
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
package Threading;
import java.util.ArrayList;
import java.util.List;
public final class IntPrinter {
public static void main(String[] args) throws Exception{
if (args.length != 1) {
System.out.println("Usage: Main <number of threads>");
return;
}
int n = Integer.parseInt(args[0]);
List <Thread> threads = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
threads.add(new Thread(new IntRunner(i)));
}
for (Thread thread : threads) thread.start();
for (Thread thread : threads) thread.join();
}
public static final class IntRunner implements Runnable {
private final int value;
IntRunner(int value) {
this.value = value;
}
@Override
public void run() {
System.out.println("Thread #" + Thread.currentThread().getId() + " printed " + value);
}
}
}