回顧java多線程中sleep的使用問題


第一是同學說的問題,主線程先跑完,新線程跑的問題,結果是新開的線程是可以和主線程PK的,本來就是很基本的問題了,感覺應該是電腦問題。

下面是測試代碼:

 1 package test;
 2 /**
 3  * @author AsceticJ
 4  * @date 2017年4月20日 下午10:25:14
 5  * @version 1.0
 6  * @TODO 測試在主線程中開啟新的線程之后,主線程和新的線程執行順序,結果說明新的線程和主線程是可以PK的
 7  */
 8 public class Test implements Runnable
 9 {
10     public static void main(String[] args)
11     {
12         Test test = new Test();
13         Thread thread = new Thread(test); 
14         thread.start();    
15         for(int i=0; i<10; i++){
16             System.out.println(Thread.currentThread().getName()+"嘿嘿嘿"+i);
17         }
18     }
19     
20     @Override
21     public void run()
22     {
23         for(int i=0; i<10; i++){
24             System.out.println(Thread.currentThread().getName()+"哈哈哈"+i);
25         }
26     }
27 
28 }

測試截圖:主線程和新的線程交替執行

 

第二個是看sleep方法不釋放鎖的問題

測試代碼:

 1 package test;
 2 /**
 3  * @author AsceticJ
 4  * @date 2017年4月20日 下午10:25:14
 5  * @version 1.0
 6  * @TODO 測試在在同步代碼塊中調用sleep()方法后,線程不會釋放鎖
 7  */
 8 public class Test1 implements Runnable
 9 {
10     private String lock = "lock";
11     private int count = 0;
12     public static void main(String[] args)
13     {
14         Test1 test = new Test1();
15         Thread thread = new Thread(test); 
16         Thread thread1 = new Thread(test); 
17         thread.start();
18         thread1.start();
19         
20         for(int i=0; i<10; i++){
21             System.out.println(Thread.currentThread().getName()+"-PK勝利-正在嘿嘿嘿!"+i);
22         }
23     }
24     
25     @Override
26     public void run()
27     {
28         System.out.println(Thread.currentThread().getName()+" PK勝利!");
29         while(count<10){
30             System.out.println(Thread.currentThread().getName()+" 開始搶鎖!");
31             synchronized (lock)
32             {
33                 System.out.println(Thread.currentThread().getName()+" 搶鎖勝利,正在嘿嘿嘿!"+(count++));
34                 try
35                 {
36                     Thread.sleep(1000);
37                 } catch (InterruptedException e)
38                 {
39                     e.printStackTrace();
40                 }
41                 System.out.println(Thread.currentThread().getName()+" 嘿嘿嘿結束,准備下一波!"+(count++));
42             }
43             
44         }
45     }
46 
47 }

測試截圖:

 

第三個是問為什么要在同步塊中使用sleep,不用sleep的時候為什么是一個線程全部執行

測試代碼,只是一個注釋了sleep一個沒注釋

 1 package test;
 2 /**
 3  * @author AsceticJ
 4  * @date 2017年4月20日 下午10:25:14
 5  * @version 1.0
 6  * @TODO 測試在在同步代碼塊中調用sleep()
 7  */
 8 public class Test3 implements Runnable
 9 {
10     private String lock = "lock";
11     private int count = 0;
12     public static void main(String[] args)
13     {
14         Test3 test = new Test3();
15         Thread thread = new Thread(test); 
16         Thread thread1 = new Thread(test); 
17         Thread thread2 = new Thread(test); 
18         thread.start();
19         thread1.start();
20         thread2.start();
21         for(int i=0; i<10; i++){
22             System.out.println(Thread.currentThread().getName()+"-PK勝利-正在嘿嘿嘿!"+i);
23         }
24     }
25     
26     @Override
27     public void run()
28     {
29         System.out.println(Thread.currentThread().getName()+" PK勝利!");
30         while(count<10){
31             System.out.println(Thread.currentThread().getName()+" 開始搶鎖!");
32             synchronized (lock)
33             {
34                 System.out.println(Thread.currentThread().getName()+" 搶鎖勝利,正在嘿嘿嘿!"+(count++));
35 //                try
36 //                {
37 //                    Thread.sleep(1000);
38 //                } catch (InterruptedException e)
39 //                {
40 //                    e.printStackTrace();
41 //                }
42                 System.out.println(Thread.currentThread().getName()+" 嘿嘿嘿結束,准備下一波!"+(count++));
43             }
44             
45         }
46     }
47 
48 }

測試截圖:

未sleep:

sleep:

 最后附一張線程啟動順序的示意圖


免責聲明!

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



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