-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountAndPrint.java
More file actions
34 lines (28 loc) · 843 Bytes
/
CountAndPrint.java
File metadata and controls
34 lines (28 loc) · 843 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
33
34
package src;
class CountAndPrint implements Runnable {
private final String name;
CountAndPrint(String name) {
this.name = name;
}
/**
* This is what a src.CountAndPrint will do
*/
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(this.name + ": " + i);
}
}
public static void main(String[] args) {
// Launching 4 parallel threads
for (int i = 1; i <= 4; i++) {
// `start` method will call the `run` method
// of src.CountAndPrint in another thread
new Thread(new CountAndPrint("Instance " + i)).start();
}
// Doing some others tasks in the main Thread
for (int i = 0; i < 100; i++) {
System.out.println("src.Main: " + i);
}
}
}