面试Java学习笔记程序员

线程通信

2017-02-01  本文已影响69人  小鱼嘻嘻

1 wait和notify

首先需要注意的是wait和notify都不在thread里,而是在Object里为什么呢?
原因是因为:wait和notify的使用时针对锁的,如果没有synchronized,wait和notify也起不到线程通信的作用,而synchronized是对象锁,是针对对象的,所以wait和notify放在Object里面显得更合理。

package com.yuxi;

/**
 * Created by yuxi on 17/1/31.
 */
public class WaitDemo {
    public static void main(String[] args) {
        Object object = new Object();
        MyThread myThread = new MyThread(object);
        new Thread(myThread).start();
//        new Thread(myThread).start();
        //需要注意的是wait和notify都必须在synchronized里面,否则就会抛出异常
        try {
            Thread.sleep(0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (object) {
            System.out.println("因为wait释放锁了所有main线程可以立刻拿到" + Thread.currentThread().getName() + "..." + System.currentTimeMillis());
            object.notify();
            System.out.println("因为wait释放锁了所有main线程可以立刻拿到" + Thread.currentThread().getName() + "..." + System.currentTimeMillis());
        }

    }


    static class MyThread implements Runnable {
        private Object object;

        public MyThread(Object object) {
            this.object = object;
        }

        public void run() {
            synchronized (object) {
                try {
                    System.out.println("wait....." + Thread.currentThread().getName() + "..." + System.currentTimeMillis());
                    object.wait();
                    Thread.sleep(2000);
                    System.out.println("wait....." + Thread.currentThread().getName() + "..." + System.currentTimeMillis());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}

结果是:

wait.....Thread-0...1485911417379
因为wait释放锁了所有main线程可以立刻拿到main...1485911417380
因为wait释放锁了所有main线程可以立刻拿到main...1485911417380
wait.....Thread-0...1485911419384

从结果可以看出:
wait之后锁肯定释放,不然notify的synchronized是无法拿到锁的。

2 wait和notify实例

来模拟一个线程通信的实例:

package com.yuxi;

/**
 * Created by yuxi on 17/2/1.
 */
public class TxDemo {
    public static void main(String[] args) {
        Object object = new Object();
        String name = "yuxi";
        ThreadA threadA = new ThreadA(object,null);
        ThreadB threadB = new ThreadB(object,name);
        new Thread(threadA).start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(threadB).start();
    }

    static class ThreadA implements Runnable {
        private Object object;
        private String name;

        public ThreadA(Object object, String name) {
            this.object = object;
            this.name = name;
        }

        public void run() {
            try {
                synchronized (object) {
                    if (name == null) {
                        System.out.println( Thread.currentThread().getName() + "..." + System.currentTimeMillis());
                        object.wait();
                        System.out.println("..."+Thread.currentThread().getName()+".....wait");
                        System.out.println( Thread.currentThread().getName() + "..." + System.currentTimeMillis());
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class ThreadB implements Runnable {
        private Object object;
        private String name;

        public ThreadB(Object object, String name) {
            this.object = object;
            this.name = name;
        }

        public void run() {
            synchronized (object) {
                if (name != null) {
                    System.out.println( Thread.currentThread().getName() + "..." + System.currentTimeMillis());
                    object.notify();
                    System.out.println("..."+Thread.currentThread().getName()+".....notify");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println( Thread.currentThread().getName() + "..." + System.currentTimeMillis());
                }
            }
        }
    }
}

例子很简单,一个线程A因为没有值处于等待状态,另外一个线程B拿到值之后唤醒线程A,使得两个线程直接完成通信。
结果:

Thread-0...1485913497377
Thread-1...1485913497481
...Thread-1.....notify
Thread-1...1485913502483
...Thread-0.....wait
Thread-0...1485913502483

3 wait和sleep,join区别

上一篇下一篇

猜你喜欢

热点阅读