转自:http://my.oschina.net/bairrfhoinn/blog/164737
这两天在研究多线程的使用方法,我们的需求是这样的:程序启动时,先让子线程处于运行状态,运行过程中会人为的更改该线程的运行状态为挂起,等待随机的时长之后,再把该线程重新唤醒,让其继续运行;人为挂起线程和恢复线程的次数也是随机的。
经过不懈努力,最终找到了如下壹個实现了 Runnable 的线程类,完美解决该问题,记录于此。首先是 MyThread 线程类的定义:
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
|
public
class
MyThread
implements
Runnable {
private
boolean
running =
false
;
private
boolean
waiting =
false
;
private
Thread thread;
private
String name;
public
MyThread(String name) {
this
.name = name;
this
.thread =
new
Thread(
this
);
}
//启动线程
public
void
start() {
running =
true
;
thread.start();
}
//挂起线程
public
void
suspend() {
if
(waiting) {
return
;
}
synchronized
(
this
) {
this
.waiting =
true
;
}
}
//恢复线程
public
void
resume() {
if
(!waiting) {
return
;
}
synchronized
(
this
) {
this
.waiting =
false
;
this
.notifyAll();
}
}
//终止线程
public
void
stop() {
if
(!running) {
return
;
}
synchronized
(
this
) {
running =
false
;
}
}
public
void
run() {
while
(
true
) {
try
{
// 线程挂起和退出处理
synchronized
(
this
) {
if
(!running) {
break
;
}
if
(waiting) {
this
.wait();
}
}
// 应该做的事情
execute();
// 进入等待状态
Thread.sleep(
50
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
}
//真正要做的事情
private
void
execute() {
if
(running && !waiting){
System.out.println(name +
" is running..."
);
}
}
}
|
上述代码中,MyThread 类提供了四個 public 的方法,分别是 start(),suspend(),resume(),stop(),分别完成启动线程,挂起线程,恢复线程,终止线程的功能,程序通过两個布尔型变量控制子线程 thread 的状态:waiting 和 running,彼此之间配合堪称天衣无缝, 多线程果然强大。
接下来是调用该线程类的测试代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public
class
Main {
public
static
void
main(String[] args) {
try
{
MyThread dog =
new
MyThread(
"test"
);
System.out.println(
"--- start the subthread"
);
dog.start();
Thread.sleep(
500
);
System.out.println(
"--- suspend the thread"
);
dog.suspend();
System.out.println(
"--- main thread do something"
);
Thread.sleep(
500
);
System.out.println(
"--- resume the thread"
);
dog.resume();
Thread.sleep(
500
);
System.out.println(
"--- end this thread"
);
dog.stop();
System.out.println(
"--- main thread do something"
);
Thread.sleep(
100
);
System.out.println(
"--- exit programe."
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
}
|
该类运行之后的输出结果为:
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
|
--- start the subthread
test is running...
test is running...
test is running...
test is running...
test is running...
test is running...
test is running...
test is running...
test is running...
test is running...
test is running...
--- suspend the thread
--- main thread
do
something
--- resume the thread
test is running...
test is running...
test is running...
test is running...
test is running...
test is running...
test is running...
test is running...
test is running...
test is running...
--- end
this
thread
--- main thread
do
something
--- exit programe.
|