这两天在研究多线程的使用方法,我们的需求是这样的:程序启动时,先让子线程处于运行状态,运行过程中会人为的更改该线程的运行状态为挂起,等待随机的时长之后,再把该线程重新唤醒,让其继续运行;人为挂起线程和恢复线程的次数也是随机的。
经过不懈努力,最终找到了如下壹個实现了 Runnable 的线程类,完美解决该问题,记录于此。首先是 MyThread 线程类的定义:
01 | public class MyThread implements Runnable { |
02 |
03 | private boolean running = false ; |
04 | private boolean waiting = false ; |
05 | private Thread thread; |
06 | private String name; |
07 |
08 | public MyThread(String name) { |
09 | this .name = name; |
10 | this .thread = new Thread( this ); |
11 | } |
12 |
13 | //启动线程 |
14 | public void start() { |
15 | running = true ; |
16 | thread.start(); |
17 | } |
18 |
19 | //挂起线程 |
20 | public void suspend() { |
21 | if (waiting) { |
22 | return ; |
23 | } |
24 | synchronized ( this ) { |
25 | this .waiting = true ; |
26 | } |
27 | } |
28 |
29 | //恢复线程 |
30 | public void resume() { |
31 | if (!waiting) { |
32 | return ; |
33 | } |
34 | synchronized ( this ) { |
35 | this .waiting = false ; |
36 | this .notifyAll(); |
37 | } |
38 | } |
39 |
40 | //终止线程 |
41 | public void stop() { |
42 | if (!running) { |
43 | return ; |
44 | } |
45 | synchronized ( this ) { |
46 | running = false ; |
47 | } |
48 | } |
49 |
50 | public void run() { |
51 | while ( true ) { |
52 | try { |
53 | // 线程挂起和退出处理 |
54 | synchronized ( this ) { |
55 | if (!running) { |
56 | break ; |
57 | } |
58 | if (waiting) { |
59 | this .wait(); |
60 | } |
61 | } |
62 | // 应该做的事情 |
63 | execute(); |
64 | // 进入等待状态 |
65 | Thread.sleep( 50 ); |
66 | } catch (InterruptedException e) { |
67 | e.printStackTrace(); |
68 | } |
69 | } |
70 | } |
71 | |
72 | //真正要做的事情 |
73 | private void execute() { |
74 | if (running && !waiting){ |
75 | System.out.println(name + " is running..." ); |
76 | } |
77 | } |
78 | } |
上述代码中,MyThread 类提供了四個 public 的方法,分别是 start(),suspend(),resume(),stop(),分别完成启动线程,挂起线程,恢复线程,终止线程的功能,程序通过两個布尔型变量控制子线程 thread 的状态:waiting 和 running,彼此之间配合堪称天衣无缝, 多线程果然强大。
接下来是调用该线程类的测试代码:
01 | public class Main { |
02 | public static void main(String[] args) { |
03 | try { |
04 | MyThread dog = new MyThread( "test" ); |
05 | System.out.println( "--- start the subthread" ); |
06 | dog.start(); |
07 | Thread.sleep( 500 ); |
08 | System.out.println( "--- suspend the thread" ); |
09 | dog.suspend(); |
10 | System.out.println( "--- main thread do something" ); |
11 | Thread.sleep( 500 ); |
12 | System.out.println( "--- resume the thread" ); |
13 | dog.resume(); |
14 | Thread.sleep( 500 ); |
15 | System.out.println( "--- end this thread" ); |
16 | dog.stop(); |
17 | System.out.println( "--- main thread do something" ); |
18 | Thread.sleep( 100 ); |
19 | System.out.println( "--- exit programe." ); |
20 | } catch (InterruptedException e) { |
21 | e.printStackTrace(); |
22 | } |
23 | } |
24 | } |
该类运行之后的输出结果为:
01 | --- start the subthread |
02 | test is running... |
03 | test is running... |
04 | test is running... |
05 | test is running... |
06 | test is running... |
07 | test is running... |
08 | test is running... |
09 | test is running... |
10 | test is running... |
11 | test is running... |
12 | test is running... |
13 | --- suspend the thread |
14 | --- main thread do something |
15 | --- resume the thread |
16 | test is running... |
17 | test is running... |
18 | test is running... |
19 | test is running... |
20 | test is running... |
21 | test is running... |
22 | test is running... |
23 | test is running... |
24 | test is running... |
25 | test is running... |
26 | --- end this thread |
27 | --- main thread do something |
28 | --- exit programe. |