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,要不达不到预想效果

你可能感兴趣的:(java,class,Java)