java notify和wait

package com.data;
public class Test {
    public static class AstronLock{
    }

    public static class Produce implements Runnable{
        private AstronLock astronLock;
        Produce(AstronLock astronLock){
            this.astronLock = astronLock;
        }
        @Override
        public  void run()  {
            System.out.println("生产者开始生产");
            synchronized(astronLock) {
                try {
                    Thread.sleep(1000);
                    astronLock.notifyAll();
                System.out.println("生产者生产,完成");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static class Consumer implements Runnable{
        private AstronLock astronLock;
        Consumer(AstronLock astronLock){
            this.astronLock =  astronLock;
        }

        @Override
        public  void run() {
            System.out.println(Thread.currentThread().getName()+"消费者申请消费");
            synchronized(astronLock) {
                try {
                    astronLock.wait();

                System.out.println(Thread.currentThread().getName()+"消费者完成");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws Exception{
        AstronLock astronLock = new  AstronLock();
      /*  Produce pro = new Produce(astronLock);
        Consumer consumer = new Consumer(astronLock);*/
        new Thread(new Consumer(astronLock)).start();
        new Thread(new Consumer(astronLock)).start();
        new Thread(new Consumer(astronLock)).start();
        Thread.sleep(1000);
        new Thread(new Produce(astronLock)).start();
    }
}

Thread-0消费者申请消费
Thread-2消费者申请消费
Thread-1消费者申请消费
生产者开始生产
生产者生产,完成
Thread-1消费者完成
Thread-2消费者完成
Thread-0消费者完成

改下notifyAll只有notify1个完成

package com.data;

public class Test {
    public static class AstronLock{
    }

    public static class Produce implements Runnable{
        private AstronLock astronLock;
        Produce(AstronLock astronLock){
            this.astronLock = astronLock;
        }
        @Override
        public  void run()  {
            System.out.println("生产者开始生产");
            synchronized(astronLock) {
                try {
                    Thread.sleep(1000);
                    astronLock.notify();
                System.out.println("生产者生产,完成");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static class Consumer implements Runnable{
        private AstronLock astronLock;
        Consumer(AstronLock astronLock){
            this.astronLock =  astronLock;
        }

        @Override
        public  void run() {
            System.out.println(Thread.currentThread().getName()+"消费者申请消费");
            synchronized(astronLock) {
                try {
                    astronLock.wait();

                System.out.println(Thread.currentThread().getName()+"消费者完成");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws Exception{
        AstronLock astronLock = new  AstronLock();
      /*  Produce pro = new Produce(astronLock);
        Consumer consumer = new Consumer(astronLock);*/
        new Thread(new Consumer(astronLock)).start();
        new Thread(new Consumer(astronLock)).start();
        new Thread(new Consumer(astronLock)).start();
        Thread.sleep(1000);
        new Thread(new Produce(astronLock)).start();
    }
}

Thread-0消费者申请消费
Thread-1消费者申请消费
Thread-2消费者申请消费
生产者开始生产
生产者生产,完成
Thread-0消费者完成

  public static class Consumer implements Runnable{
        private AstronLock astronLock;
        Consumer(AstronLock astronLock){
            this.astronLock =  astronLock;
        }

        @Override
        public  void run() {
            System.out.println(Thread.currentThread().getName()+"消费者申请消费");
            synchronized(astronLock) {
                try {
                    astronLock.wait();
                    Thread.sleep(10000);

                System.out.println(Thread.currentThread().getName()+"消费者完成在"+System.currentTimeMillis());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
Thread-0消费者申请消费
Thread-2消费者申请消费
Thread-1消费者申请消费
生产者开始生产
生产者生产,完成在1516265168543
Thread-1消费者完成在1516265178544
Thread-2消费者完成在1516265188545
Thread-0消费者完成在1516265198546

这里面可以看出Thread.sleep(10000);并不会释放对象锁。所以每个线程都是带着锁sleep了10秒才释放锁。

引入概念。对象的锁池和等待池。

main中有个sleep。是因为如果通知过早,会打乱程序的执行逻辑。应该先wait再用notify或者notifyAll,要不达不到预想效果

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值