-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreads2.java
More file actions
65 lines (53 loc) · 1.25 KB
/
Threads2.java
File metadata and controls
65 lines (53 loc) · 1.25 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
import java.util.*;
class ExampleThread implements Runnable {
private Thread thread;
private int counter;
static boolean stop = true;
ExampleThread(String s) {
thread = new Thread(this, s);
counter = 0;
}
//factory method
static ExampleThread factoryExample(String n) {
var th = new ExampleThread(n);
th.thread.start();
return th;
}
public void run() {
while (counter < 10_000_000 && stop) {
counter++;
}
stop = false;
}
Thread getThread() {
return thread;
}
int getCount() {
return counter;
}
}
class Threads2 {
public static void main(String[] sth) {
threading();
}
static void threading() {
var r = new Random();
int n = 5;
var t = new ExampleThread[n];
for (int i = 0; i < n; i++) {
t[i] = ExampleThread.factoryExample("#Child " + (i+1));
//t[i].getThread().setPriority(r.nextInt(9) + 1);
System.out.println("Priority for #Child " + (i+1) + ": " + t[i].getThread().getPriority());
}
System.out.println("\n\n");
while (ExampleThread.stop) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Error: " + e);
}
}
for (var v : t) System.out.println(v.getThread().getName() + " has count " + v.getCount());
System.out.println("\nMain method done");
}
}