在一个类中写完多线程(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

转载于:https://www.cnblogs.com/sbclmy/p/10854030.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值