1. 程式人生 > >在一個類中寫完多線程(sleep 方法和wait 方法的區別)

在一個類中寫完多線程(sleep 方法和wait 方法的區別)

對象 div method pri stat 展示 done lock star

繼承Thread()類完成多線程,一般需要4步:

(1)新建一個MyThread類繼承Thread類

(2)在MyThread類中重寫run()方法

(3)在主線程中創建線程對象 Thread t = new Thread();

(4)在主線程中啟動線程t.start()

上述過程,要分別在兩個類中實現。

下面的將展示在一個類中實現

 1 public class SleepWaitDemo {
 2     public static void main(String[] args) {
 3         final Object lock = new Object();
4 // 創建A線程,並啟動 5 new Thread(new Runnable() { 6 public void run() { 7 System.out.println("thread A is waiting to get lock"); 8 synchronized (lock) { 9 try { 10 System.out.println("thread A get lock");
11 Thread.sleep(20);//sleep()僅釋放CPU但不釋放鎖 12 System.out.println("thread A do wait method"); 13 lock.wait(1000);//wait()釋放CPU和鎖 14 System.out.println("thread A is done"); 15 } catch (InterruptedException e) {
16 e.printStackTrace(); 17 } 18 } 19 } 20 }).start();// 創建A線程並啟動完畢 21 22 try { 23 Thread.sleep(10);//主線程sleep 10ms 24 } catch (InterruptedException e1) { 25 e1.printStackTrace(); 26 } 27 28 // 創建B線程,並啟動 29 new Thread(new Runnable() { 30 public void run() { 31 System.out.println("thread B is waiting to get lock"); 32 synchronized (lock) { 33 try { 34 System.out.println("thread B get lock"); 35 System.out.println("thread B is sleeping 10ms"); 36 Thread.sleep(10); 37 System.out.println("thread B is done"); 38 } catch (InterruptedException e) { 39 e.printStackTrace(); 40 } 41 } 42 } 43 }).start();// 創建B線程並啟動完畢 44 } 45 }

結果為:

thread A is waiting to get lock

thread B is waiting to get lock

thread A do wait method

thread B get lock

thread B is sleeping 10ms

thread B is done

thread A is done

第二個例子

 1 public class SleepWaitDemo {
 2     public static void main(String[] args) {
 3         final Object lock = new Object();
 4         // 創建A線程,並啟動
 5         new Thread(new Runnable() {
 6             public void run() {
 7                 System.out.println("thread A is waiting to  get lock");
 8                 synchronized (lock) {
 9                     try {
10                         System.out.println("thread A get lock");
11                         Thread.sleep(20);//sleep()僅釋放CPU但不釋放鎖
12                         System.out.println("thread A  do wait method");
13                         Thread.sleep(1000);
14                         System.out.println("thread A is done");
15                     } catch (InterruptedException e) {
16                         e.printStackTrace();
17                     }
18                 }
19             }
20         }).start();// 創建A線程並啟動完畢
21         
22         try {
23             Thread.sleep(10);//主線程sleep 10ms
24         } catch (InterruptedException e1) {
25             e1.printStackTrace();
26         }
27         
28         // 創建B線程,並啟動
29                 new Thread(new Runnable() {
30                     public void run() {
31                         System.out.println("thread B is waiting to get lock");
32                         synchronized (lock) {
33                             try {
34                                 System.out.println("thread B  get lock");
35                                 System.out.println("thread B is sleeping 10ms");
36                                 lock.wait(10);//wait()釋放CPU和鎖
37                                 System.out.println("thread B is done");
38                             } catch (InterruptedException e) {
39                                 e.printStackTrace();
40                             }
41                         }
42                     }
43                 }).start();// 創建B線程並啟動完畢
44     }
45 }

結果:

thread A is waiting to get lock

thread A get lock

thread B is waiting to get lock

thread A do wait method

thread A is done

thread B get lock

thread B is sleeping 10ms

thread B is done

在一個類中寫完多線程(sleep 方法和wait 方法的區別)