零EVA

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

stop方法已经过时

如何停止线程?

只有一种,run方法结束

开始多线程运行,运行代码通常是循环结构

只要控制住循环,就可以让run方法结束,也就是线程结束

 

特殊情况

当线程处于冻结状态

就不会读取到标记,那么线程就不会结束

当没有指定的方式让冻结的线程恢复到运行状态时,这时就需要对冻结状态进行清除

强制让线程恢复到运行状态中来,这样就可以操作标记让线程结束

Thread类中提供了该方法interrupt();

 1 class StopThread implements Runnable
 2 {
 3     private boolean flag = true;
 4     public synchronized void run()
 5     {
 6         while(flag)
 7         {
 8             try
 9             {
10                 wait();
11             }
12             catch(InterruptedException e)
13             {
14                 System.out.println(Thread.currentThread().getName()+"...InterruptedException");
15                 flag = false;
16             }
17             System.out.println(Thread.currentThread().getName()+"...run");
18         }
19     }
20     public void changeFlag()
21     {
22         flag = false;
23     }
24 }
25 class StopThreadDemo
26 {
27     public static void main(String[] args)
28     {
29         StopThread st = new StopThread();
30         Thread t1 = new Thread(st);
31         Thread t2 = new Thread(st);
32         
33         t1.start();
34         t2.start();
35         
36         int num = 0;
37         while(true)
38         {
39             if(num++ == 60)
40             {
41                 //st.changeFlag();
42                 t1.interrupt();
43                 break;
44             }
45             System.out. println(Thread.currentThread().getName()+"...num");
46         }
47         System.out.println("over");
48     }
49 }
View Code

 守护线程需要在线程启动前设置

当运行的线程都是守护线程的时候,JAVA虚拟机退出

 1 class StopThread implements Runnable
 2 {
 3     private boolean flag = true;
 4     public synchronized void run()
 5     {
 6         while(flag)
 7         {
 8             try
 9             {
10                 wait();
11             }
12             catch(InterruptedException e)
13             {
14                 System.out.println(Thread.currentThread().getName()+"...InterruptedException");
15                 flag = false;
16             }
17             System.out.println(Thread.currentThread().getName()+"...run");
18         }
19     }
20     public void changeFlag()
21     {
22         flag = false;
23     }
24 }
25 class StopThreadDemo
26 {
27     public static void main(String[] args)
28     {
29         StopThread st = new StopThread();
30         Thread t1 = new Thread(st);
31         Thread t2 = new Thread(st);
32         
33         t1.setDaemon(true);
34         t1.setDaemon(true);
35         t1.start();
36         t2.start();
37         
38         int num = 0;
39         while(true)
40         {
41             if(num++ == 60)
42             {
43                 //st.changeFlag();
44                 t1.interrupt();
45                 break;
46             }
47             System.out. println(Thread.currentThread().getName()+"...num");
48         }
49         System.out.println("over");
50     }
51 }
View Code

 当A线程执行到了B线程的.join()方法时,A就会等待,等B线程执都行完,A才会执行

join可以用来临时加入线程执行

 1 class Demo implements Runnable
 2 {
 3     public void run()
 4     {
 5         for(int x = 0; x < 70; x++)
 6         {
 7             System.out.println(Thread.currentThread().getName()+"..."+x);
 8         }
 9     }
10 }
11 
12 class JoinDemo
13 {
14     public static void main(String[] args) throws Exception
15     {
16         Demo d = new Demo();
17         Thread t1 = new Thread(d);
18         Thread t2 = new Thread(d);
19         t1.start();
20         t1.join();
21         t2.start();
22         for(int x = 0; x < 70; x++)
23         {
24             System.out.println("main..."+x);
25         }
26         System.out.println("over");
27     }
28 }
View Code

 

posted on 2017-06-23 19:53  零EVA  阅读(458)  评论(0编辑  收藏  举报