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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM