Java学习之Thread方法

一、sleep()方法:在同步中,释放CPU执行权,不释放同步锁,意味着程序停止

二、停止线程方法:原理:run执行完成

1、设置标记

 1 class StopThread implements Runnable
 2 {
 3     private boolean flag=true;
 4     public void run()
 5     {
 6         while(flag)
 7         {
 8             System.out.println(Thread.currentThread().getName());
 9         }
10         
11     }
12     public void setFlag()
13     {
14         this.flag=false;
15     }
16 }
17 
18 class StopThreadDemo
19 {
20     public static void main(String[] args)
21     {
22         StopThread st=new StopThread();
23         
24         Thread t1=new Thread(st);
25         Thread t2=new Thread(st);
26         t1.start();
27         t2.start();
28         
29         int num = 1;
30         for(;;)
31         {
32             if(++num==50)
33             {
34                 st.setFlag();
35                 break;
36             }
37             System.out.println(Thread.currentThread().getName()+";;;;"+num);
38         }
39         System.out.println("over");
40     }
41 }

2、interrupe方法:在同步线程中,把冻结状态的线程,强制恢复到运行状态,让其获取CPU执行资格

 1 /**
 2 停止线程方法
 3 1、run方法执行完成
 4 2、设置标记
 5 3、interrupt()方法
 6 4、setDaemon()
 7 
 8 */
 9 
10 //
11 class StopThread implements Runnable
12 {
13     private boolean flag=true;
14     public void run()
15     {
16         while(flag)
17         {
18             show();
19         }
20         
21     }
22     
23     private synchronized void show()
24     {
25         try{this.wait();}catch(InterruptedException e){this.flag=false;}
26         System.out.println(Thread.currentThread().getName());
27     }
28     
29     public void setFlag()
30     {
31         this.flag=false;
32     }
33 }
34 
35 class StopThreadDemo
36 {
37     public static void main(String[] args)
38     {
39         StopThread st=new StopThread();
40         
41         Thread t1=new Thread(st);
42         Thread t2=new Thread(st);
43         t1.start();
44         t2.start();
45         
46         int num = 1;
47         for(;;)
48         {
49             if(++num==50)
50             {
51                 //st.setFlag();
52                 t1.interrupt();
53                 t2.interrupt();
54                 break;
55             }
56             System.out.println(Thread.currentThread().getName()+";;;;"+num);
57         }
58         System.out.println("over");
59     }
60 }

3、setDaemon(true):设置守护线程,后台线程

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

所有非后台线程都执行完成,后台现在无论处于什么状态都会退出。

三、join方法

 1 class Demo implements Runnable
 2 {
 3     public void run()
 4     {
 5         for(int x=0;x<50;x++)
 6         {
 7             System.out.println(Thread.currentThread().getName()+"...."+x);
 8         }
 9     }
10 }
11 
12 //将执行join()方法的线程终止,释放其CPU执行权和执行资格,等待join()方法所属的线程执行完成后,才能重新获取CPU执行资格
13 class JoinDemo
14 {
15     public static void main(String[] args)throws Exception
16     {
17         Demo st=new Demo();
18         
19         Thread t1=new Thread(st);
20         Thread t2=new Thread(st);
21         t1.start();
22         t1.join(); //main 线程终止,等待t1线程执行完成才继续执行。
23         t2.start();
24         //t1.join();//main 线程终止,t1和t2抢夺CPU执行权,main线程等待t1线程执行完成才继续执行。
25         for(int x=0;x<50;x++)
26         {
27             System.out.println(Thread.currentThread().getName()+"...."+x);
28         }
29         System.out.println("over");
30     }
31 }

四、yield方法

 1 class Demo implements Runnable
 2 {
 3     public void run()
 4     {
 5         for(int i=0;x<50;x++)
 6         {
 7             System.out.println(Thread.currentThread().getName()+"...."+x);
 8             Thread.yield();//释放CPU执行权,注意:释放CPU执行权,不代表它自己不能再次获取CPU执行权
 9         }
10     }
11 }
原文地址:https://www.cnblogs.com/WarBlog/p/12084106.html