多线程编程<三>

 1 /**
 2  * 线程的暂停、恢复和停止
 3  * @author Administrator
 4  *
 5  */
 6 public class ThreadControlDemo {
 7     public static void main(String[] args) {
 8         MyThread mt = new MyThread("MyThread");
 9         
10         try {
11             Thread.sleep(3000);
12             System.out.println("\nSuspending MyThread.");
13             mt.mySuspend();
14             Thread.sleep(3000);
15             
16             System.out.println("\nResuming MyThread.");
17             mt.myResume();
18             Thread.sleep(3000);
19             
20             System.out.println("\nSuspending MyThread again.");
21             mt.mySuspend();
22             Thread.sleep(3000);
23             
24             System.out.println("\nResuming MyThread again.");
25             mt.myResume();
26             Thread.sleep(3000);
27             
28             System.out.println("\nStopping MyThread.");
29             mt.myStop();
30             
31         } catch (InterruptedException e) {
32             System.out.println("Main thread Interrupted");
33         }
34     }
35 }
36 
37 class MyThread implements Runnable {
38     Thread thrd;
39     private volatile boolean suspended;//暂停
40     private volatile boolean stopped;
41     
42     MyThread(String name){
43         thrd = new Thread(this, name);
44         suspended = false;
45         stopped = false;
46         thrd.start();
47     }
48     @Override
49     public void run() {
50         System.out.println(thrd.getName() + " starting.");
51         try {
52             for (int i = 0; i < 1000; i++) {
53                 if (stopped) {
54                     System.out.println("Stopping begin.(1):");
55                 }
56                 System.out.print(".");
57                 Thread.sleep(300);
58                 if (stopped) {
59                     System.out.println("Stopping begin.(2):");
60                 }
61                 synchronized (this) {
62                     while(suspended) wait();
63                     
64                     //如果thread要停止,跳出循环,线程结束。
65                     if (stopped) {
66                         break;
67                     }
68                 }
69             }
70         } catch (InterruptedException e) {
71             System.out.println(thrd.getName() + " interrupted.");
72         }
73         System.out.println("\n" + thrd.getName() + " exiting.");
74     }
75     
76     //调用方法停止线程
77     synchronized void myStop() {
78         stopped = true;
79         
80         suspended = false;
81         notify();
82     }
83     
84     //暂停线程
85     synchronized void mySuspend() {
86         suspended = true;
87     }
88     
89     //恢复线程
90     synchronized void myResume(){
91         suspended = false;
92         notify();
93     }
94 }

结果:

MyThread starting.
..........
Suspending MyThread.

Resuming MyThread.
...........
Suspending MyThread again.

Resuming MyThread again.
...........
Stopping MyThread.
Stopping begin.(2):

MyThread exiting.

posted @ 2014-05-14 17:01  soul390  阅读(146)  评论(0编辑  收藏  举报