• java多线程之wait和notify


    多线程中的通信是非常重要的概念,线程直接实现通信就可以并发完成很多复杂工作。

    java在Object类中就设计了wait()和notify()两个方法,以解决这个问题。

    1.释义:

    wait()方法将当前线程暂停,置于“预执行队列”中,而notify()则用于通知一个在wait等待中的线程,可以继续执行了

    2.wait()和notify()的使用条件:

    wait()和notify()必须置于同步方法和同步代码块中使用,即在调用前,线程必须获得该对象的对象级别锁。否则会抛出异常

    3.线程执行到wait()时,直接释放锁,处于等待状态;

    public class WaitService {
        public void testMethod(Object lock) {
            try {
                synchronized (lock) {
                    System.out.println("wait begin");
                    lock.wait();
                    System.out.println("wait end");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    public class WaitThread1 extends Thread {
        private Object lock;
    
        public WaitThread1(Object lock) {
            this.lock = lock;
        }
    
        @Override
        public void run() {
            WaitService service = new WaitService();
            service.testMethod(lock);
        }
    }
    
    public class WaitThread2 extends Thread {
        private Object lock;
    
        public WaitThread2(Object lock) {
            this.lock = lock;
        }
    
        @Override
        public void run() {
            WaitService service = new WaitService();
            service.testMethod(lock);
        }
    }
    
    public class WaitTest {
        public static void main(String[] args) {
            Object lock=new Object();
            WaitThread1 t1=new WaitThread1(lock);
            WaitThread2 t2 = new WaitThread2(lock);
            t1.start();
            t2.start();
        }
    }

    执行结果:

    wait begin
    wait begin

    4.线程执行notify时,当前线程并不马上释放锁,wait状态的线程也不会马上获得锁,要执行notify方法的程序执行完,退出syncronized代码块后,才释放锁。

    public class NotifyService {
        public void testMethod(Object lock){
            try {
                synchronized (lock){
                    System.out.println("wait  begin");
                    lock.wait();
                    System.out.println("wait  end");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        public void notifyMethod(Object lock){
            try {
                synchronized (lock) {
                    System.out.println("begin nofity ");
                    Thread.sleep(1000);
                    lock.notify();
                    System.out.println("end nofity ");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    public class NotifyThreadA extends Thread{
        private Object lock;
    
        public NotifyThreadA(Object lock) {
            this.lock = lock;
        }
    
        @Override
        public void run() {
            NotifyService service=new NotifyService();
            service.testMethod(lock);
        }
    }
    
    public class NotifyThreadB extends Thread {
        private Object lock;
    
        public NotifyThreadB(Object lock) {
            this.lock = lock;
        }
    
        @Override
        public void run() {
            NotifyService service=new NotifyService();
            service.notifyMethod(lock);
        }
    }
    
    public class NotifyTest {
        public static void main(String[] args) {
            Object lock=new Object();
            NotifyThreadA a = new NotifyThreadA(lock);
            a.start();
            NotifyThreadB b = new NotifyThreadB(lock);
            b.start();
        }
    }

    输出:

    wait  begin
    begin nofity 
    end nofity 
    wait  end
  • 相关阅读:
    Centos7安装teamviewer 32/64位
    Centos7安装32位库用来安装32位软件程序
    linux环境下配置mysql双主复制
    zabbix自定义触发器进行监控
    VM安装系统时提示硬件不支持(unsupported hardware detected)
    mysql新建用户在本地无法登录
    msyql开启慢查询以及分析慢查询
    zabbix怎么把英文界面换成中文
    xencenter如何安装系统
    mysql优化查询
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/9290575.html
Copyright © 2020-2023  润新知