-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoThreadGroup
More file actions
117 lines (94 loc) · 1.88 KB
/
DemoThreadGroup
File metadata and controls
117 lines (94 loc) · 1.88 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com;
import java.awt.*;
import java.awt.event.*;
public class DemoThreadGroup extends Frame implements Runnable,ActionListener {
int x1,x2,x3;
Button b1,b2;
Thread t1,t2,t3;
ThreadGroup tg;
public DemoThreadGroup() {
setLayout(new FlowLayout());
x1=50;
x2=50;
x3=50;
tg=new ThreadGroup("tg");
t1=new Thread(tg,this);
t2=new Thread(tg,this);
t3=new Thread(tg,this);
b1=new Button("Start");
b2=new Button("Stop");
add(b1);
add(b2);
t1.start();
t2.start();
t3.start();
b1.addActionListener(this);
b2.addActionListener(this);
setSize(500,500);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we)
{
dispose();
}
});
}
public static void main(String[] args) {
new DemoThreadGroup();
}
public void run() {
while(true)
{
if(Thread.currentThread().equals(t1))
{
x1=x1+2;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(Thread.currentThread().equals(t2))
{
x2=x2+10;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(Thread.currentThread().equals(t3))
{
x3=x3+5;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
repaint();
}
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(x1, 150, 50, 50);
g.setColor(Color.green);
g.fillOval(x2, 350, 50, 50);
g.setColor(Color.blue);
g.fillOval(x3, 250 , 50, 50);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(b1))
{
tg.resume();
}
if(e.getSource().equals(b2))
{
tg.suspend();
}
}
}