-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThrGroup.java
More file actions
83 lines (76 loc) · 2.69 KB
/
ThrGroup.java
File metadata and controls
83 lines (76 loc) · 2.69 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
package multiThreading;
public class ThrGroup extends Thread {
public static void main(String[] args) {
ThreadGroup tg1 = new ThreadGroup("tg1");
ThreadGroup tg2 = new ThreadGroup(tg1, "tg2");
ThrD td = new ThrD();
RunD rd = new RunD(tg2, "rd");
Thread t1 = new Thread(tg1, td, "Thread 1 Group 1");
Thread t2 = new Thread(tg1, td, "Thread 2 Group 1");
Thread t3 = new Thread(tg2, rd, "Thread 1 Group 2");
Thread t4 = new Thread(tg2, rd, "Thread 2 Group 2");
t1.start();
t2.start();
t3.start();
t4.start();
System.out.println(tg1.activeCount());
System.out.println(tg2.activeCount());
System.out.println(tg1.activeGroupCount());
System.out.println(tg2.activeGroupCount());
tg1.checkAccess();
System.out.println(tg1.getName() + " has access");
// tg1.destroy();
// System.out.println(tg1.isDestroyed());
Thread[] tarr = new Thread[tg2.activeCount()];
int ac = tg2.enumerate(tarr);
System.out.println("tarr " + ac);
for (int i = 0; i < tg2.activeCount(); i++) {
System.out.println("@ " + tarr[i]);
}
System.out.println("tg1 maxPriority:" + tg1.getMaxPriority());
System.out.println("tg2 maxPriority:" + tg2.getMaxPriority());
System.out.println("tg1 group name: " + tg1.getName());
System.out.println("tg2 group name: " + t4.getThreadGroup().getName());
System.out.println("tg2 parent group: " + tg2.getParent());
tg2.interrupt();
tg1.interrupt();
System.out.println(tg2.isDaemon());
tg2.setDaemon(true);
System.out.println(tg2.isDaemon());
System.out.println(tg1.isDaemon());
tg2.setDaemon(false);
System.out.println(tg2.isDaemon());
tg1.list();
tg2.list();
System.out.println(tg1.parentOf(tg2));
tg2.setMaxPriority(NORM_PRIORITY);
System.out.println("tg2 max prio "+tg2.getMaxPriority());
System.out.println(tg1.toString());
tg2.stop();
}
}
class ThrD implements Runnable {
@Override
public void run() {
System.out.println("implements Runnable");
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println("sleep interruption Runnable");
}
}
}
class RunD extends Thread {
public RunD(ThreadGroup tg, String name) {
super(tg, name);
}
@Override
public void run() {
System.out.println("extends Thread");
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println("sleep interruption THread");
}
}
}