Java wait和notify方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chennai1101/article/details/84744748

1. wait和notify方法

wait方法可以使当前执行代码的线程进行等待,notify方法用来通知哪些等待该对象锁的其它线程。waitnotify方法必须要获得该对象的锁,才能运行,否则会报IllegalMonitorStateException的异常。

try {
    Object lock = new Object();
    synchronized (lock) {
        lock.wait();
    }
} catch (InterruptedException e) {
    e.printStackTrace();
}

2. wait方法

调用wait()方法后,会自动放弃获得的对象锁供其他使用,直到其它线程将其唤醒。而wait(long)方法,在等待指定时间后,自动进入就绪状态,重新运行。

public class ThreadWait {

    public static void main(String[] args) {
        new ThreadA().start();
        new ThreadB().start();
    }

    static class ThreadA extends Thread {

        public void run() {
            Object lock = new Object();
            synchronized (lock) {
                try {
                    System.out.println("In ThreadA begin at " + System.currentTimeMillis());
                    lock.wait();
                    System.out.println("In ThreadA end at " + System.currentTimeMillis());
                } catch (InterruptedException e) {
                }
            }
        }
    }

    static class ThreadB extends Thread {

        public void run() {
            Object lock = new Object();
            synchronized (lock) {
                try {
                    System.out.println("In ThreadB begin at " + System.currentTimeMillis());
                    lock.wait(2000);
                    System.out.println("In ThreadB end at " + System.currentTimeMillis());
                } catch (InterruptedException e) {
                }
            }
        }
    }

}

输出

In ThreadA begin at 1543389819253
In ThreadB begin at 1543389819253
In ThreadB end at 1543389821253

线程B在2秒后结束,而线程A一直没有结束

3. notify和notifyAll方法

notify方法也只能在同步方法或者同步代码块中运行,notify并不会立即释放该对象锁,直到线程将程序执行完。

public class ThreadNotify {

    public static void main(String[] args) {
        Object lock = new Object();		
        new ThreadA(lock).start();		
        new ThreadB(lock).start();
    }

    static class ThreadA extends Thread {
        Object lock;

        ThreadA(Object lock) {
            this.lock = lock;
        }

        public void run() {
            synchronized (lock) {
                try {
                    System.out.println("In ThreadA begin at " + System.currentTimeMillis());
                    lock.wait();
                    System.out.println("In ThreadA end at " + System.currentTimeMillis());
                } catch (InterruptedException e) {
                }
            }
        }
    }

    static class ThreadB extends Thread {
        Object lock;

        ThreadB(Object lock) {
            this.lock = lock;
        }

        public void run() {
            synchronized (lock) {
                System.out.println("In ThreadB begin at " + System.currentTimeMillis());
                lock.notify();
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                }
                System.out.println("In ThreadB end at " + System.currentTimeMillis());
            }
        }
    }

}

输出

In ThreadA begin at 1543390694751
In ThreadB begin at 1543390694752
In ThreadB end at 1543390699752
In ThreadA end at 1543390699752

notify方法只会唤醒等待队列中的一个线程,如果需要所有线程需要调用notifyAll

public static void main(String[] args) {
    Object lock = new Object();		
    new ThreadA(lock).start();
    new ThreadA(lock).start();
    new ThreadB(lock).start();
}

输出

In ThreadA begin at 1543391951312
In ThreadA begin at 1543391951312
In ThreadB begin at 1543391951313
In ThreadB end at 1543391956313
In ThreadA end at 1543391956313

只有一个ThreadA被唤醒,另一个仍然在等待中。

4. wait和interrupt方法

如果线程进入wait状态,调用线程的interrupt方法会发生InterruptedException异常。

public class ThreadInterruptWait {

    public static void main(String[] args) {		
        try {
            Object lock = new Object();
            ThreadA threadA = new ThreadA(lock);
            threadA.start();

            Thread.sleep(1000);
            threadA.interrupt();
        } catch (InterruptedException e) {
        }
    }

    static class ThreadA extends Thread {
        private Object lock;

        public ThreadA(Object lock) {
            this.lock = lock;
        }

        public void run() {
            System.out.println("In ThreadA start at " + System.currentTimeMillis());
            synchronized (lock) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("In ThreadA end at " + System.currentTimeMillis());
        }
    }

}

输出

In ThreadA start at 1543392357559
java.lang.InterruptedException
In ThreadA end at 1543392358561
    at java.lang.Object.wait(Native Method)
    at java.lang.Object.wait(Object.java:502)
    at com.blog.demo.thread.ThreadInterruptWait$ThreadA.run(ThreadInterruptWait.java:29)

猜你喜欢

转载自blog.csdn.net/chennai1101/article/details/84744748