在一个类中写完多线程(sleep 方法和wait 方法的区别)

继承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

posted @ 2019-05-12 22:23  IT刑天  阅读(614)  评论(0编辑  收藏  举报