【七】Java多线程之resume()、suspend()、stop()

版权声明:转载注明出处 https://blog.csdn.net/jy02268879/article/details/81592510

resume()、suspend()、stop(),这三个方法分别是重启线程、挂起线程、终止线程。

这三个方法已经弃用。

如果需要用到类似功能需要用flag来实现

public class StopThreadTest {
    public static void main(String[] args) {
        StopThread r1 = new StopThread();
        Thread t1 = new Thread(r1,"My Thread");
        t1.start();
        try {
            Thread.sleep(1000);

            r1.mysuspend();
            System.out.println("Suspending thread.");
            Thread.sleep(1000);

            r1.myresume();
            System.out.println("Resuming thread.");
            Thread.sleep(1000);

            r1.mysuspend();
            System.out.println("Suspending thread.");
            Thread.sleep(1000);

            r1.myresume();
            System.out.println("Resuming thread.");
            Thread.sleep(1000);

            r1.mysuspend();
            System.out.println("Suspending thread.");
            r1.mystop();
        }catch (Exception e){
            e.printStackTrace();
        }

        try{
            t1.join();
        }catch (Exception e){
            System.out.println("Main thread Interrupted");
        }

        System.out.println("Main thread exiting");
    }
}

class StopThread implements Runnable{
    volatile boolean suspended;
    volatile boolean stopped;

    StopThread(){
        suspended = false;
        stopped = false;
    }

    public void run(){
        System.out.println(Thread.currentThread().getName());

        try {
            for(int i=1; i <1000;i++){
                System.out.println(i+" ");
                if((i%10)==0){
                    System.out.println();
                    Thread.sleep(250);
                }

                synchronized (this){
                    while (suspended){
                        wait();
                    }
                    if(stopped)
                        break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName()+" exiting.");
    }

    synchronized void mystop(){
        stopped = true;
        suspended = false;
        notify();
    }

    synchronized void mysuspend(){
        suspended = true;
    }

    synchronized void myresume(){
        suspended = false;
        notify();
    }
}

My Thread
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 

Suspending thread.
Resuming thread.
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 

Suspending thread.
Resuming thread.
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 
117 
118 
119 
120 

Suspending thread.
My Thread exiting.
Main thread exiting

Process finished with exit code 0

 

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页