wait和notify

来源:互联网 发布:python 股票回测系统 编辑:程序博客网 时间:2024/06/16 12:13

new Thread(new Runnable() {    @Override    public void run() {        long id = Thread.currentThread().getId();        Log.i(TAG, "thread1 id :" + id + " begin");        new Thread(new Runnable() {            @Override            public void run() {                long id = Thread.currentThread().getId();                Log.i(TAG, "thread2 id :" + id + " begin");                try {                    Thread.sleep(4000);                } catch (InterruptedException e) {                    e.printStackTrace();                }                Log.i(TAG, "thread2 id :" + id + " end before synchronized");                synchronized (object){                    Log.i(TAG, "thread2 id :" + id + " end before notify");                    object.notify();                    Log.i(TAG, "thread2 id :" + id + " end after notify");                }                Log.i(TAG, "thread2 id :" + id + " end after synchronized");            }        }).start();        try {            Log.i(TAG, "thread1 id :" + id + " before synchronized");            synchronized (object){                Log.i(TAG, "thread1 id :" + id + " before wait");                object.wait();                Log.i(TAG, "thread1 id :" + id + " after wait");            }            Log.i(TAG, "thread1 id :" + id + " after synchronized");        } catch (InterruptedException e) {            e.printStackTrace();        }        Log.i(TAG, "thread1 id :" + id + " end");    }}).start();
同时启动两个线程:

10-27 15:16:36.274  4751  4948 I wp20170906-MainActivity: thread1 id :1513 begin

10-27 15:16:36.277  4751  4948 I wp20170906-MainActivity: thread1 id :1513 before synchronized 
10-27 15:16:36.277  4751  4948 I wp20170906-MainActivity: thread1 id :1513 before wait 线程1513开始wait
10-27 15:16:36.277  4751  4949 I wp20170906-MainActivity: thread2 id :1514 begin 线程1514开始执行


10-27 15:16:37.128  4751  4950 I wp20170906-MainActivity: thread1 id :1515 begin
10-27 15:16:37.129  4751  4950 I wp20170906-MainActivity: thread1 id :1515 before synchronized
10-27 15:16:37.129  4751  4950 I wp20170906-MainActivity: thread1 id :1515 before wait 线程1515开始wait
10-27 15:16:37.129  4751  4951 I wp20170906-MainActivity: thread2 id :1516 begin 线程1516开始执行


10-27 15:16:39.278  4751  4949 I wp20170906-MainActivity: thread2 id :1514 end before synchronized 线程1514执行完毕
10-27 15:16:39.278  4751  4949 I wp20170906-MainActivity: thread2 id :1514 end before notify
10-27 15:16:39.278  4751  4949 I wp20170906-MainActivity: thread2 id :1514 end after notify 线程1514执行完毕,notify持有同一个锁的其他线程执行
10-27 15:16:39.278  4751  4949 I wp20170906-MainActivity: thread2 id :1514 end after synchronized
10-27 15:16:39.279  4751  4948 I wp20170906-MainActivity: thread1 id :1513 after wait 线程1513开始执行wait后面的逻辑
10-27 15:16:39.279  4751  4948 I wp20170906-MainActivity: thread1 id :1513 after synchronized
10-27 15:16:39.279  4751  4948 I wp20170906-MainActivity: thread1 id :1513 end 线程1513执行完毕


10-27 15:16:40.130  4751  4951 I wp20170906-MainActivity: thread2 id :1516 end before synchronized
10-27 15:16:40.130  4751  4951 I wp20170906-MainActivity: thread2 id :1516 end before notify
10-27 15:16:40.130  4751  4951 I wp20170906-MainActivity: thread2 id :1516 end after notify 线程1516执行完毕,notify持有同一个锁的其他线程执行
10-27 15:16:40.130  4751  4951 I wp20170906-MainActivity: thread2 id :1516 end after synchronized 
10-27 15:16:40.130  4751  4950 I wp20170906-MainActivity: thread1 id :1515 after wait 线程1515开始执行wait后面的逻辑
10-27 15:16:40.130  4751  4950 I wp20170906-MainActivity: thread1 id :1515 after synchronized
10-27 15:16:40.130  4751  4950 I wp20170906-MainActivity: thread1 id :1515 end 线程1515执行完毕

同时启动三个线程:

10-27 15:35:35.588  6892  7055 I wp20170906-MainActivity: thread1 id :1601 begin
10-27 15:35:35.590  6892  7055 I wp20170906-MainActivity: thread1 id :1601 before synchronized
10-27 15:35:35.590  6892  7055 I wp20170906-MainActivity: thread1 id :1601 before wait
10-27 15:35:35.590  6892  7056 I wp20170906-MainActivity: thread2 id :1602 begin


10-27 15:35:36.701  6892  7059 I wp20170906-MainActivity: thread1 id :1603 begin
10-27 15:35:36.703  6892  7059 I wp20170906-MainActivity: thread1 id :1603 before synchronized
10-27 15:35:36.703  6892  7059 I wp20170906-MainActivity: thread1 id :1603 before wait
10-27 15:35:36.704  6892  7060 I wp20170906-MainActivity: thread2 id :1604 begin


10-27 15:35:38.594  6892  7062 I wp20170906-MainActivity: thread1 id :1605 begin
10-27 15:35:38.595  6892  7062 I wp20170906-MainActivity: thread1 id :1605 before synchronized
10-27 15:35:38.595  6892  7062 I wp20170906-MainActivity: thread1 id :1605 before wait
10-27 15:35:38.598  6892  7063 I wp20170906-MainActivity: thread2 id :1606 begin
10-27 15:35:39.591  6892  7056 I wp20170906-MainActivity: thread2 id :1602 end before synchronized
10-27 15:35:39.591  6892  7056 I wp20170906-MainActivity: thread2 id :1602 end before notify
10-27 15:35:39.591  6892  7056 I wp20170906-MainActivity: thread2 id :1602 end after notify
10-27 15:35:39.591  6892  7056 I wp20170906-MainActivity: thread2 id :1602 end after synchronized
10-27 15:35:39.592  6892  7055 I wp20170906-MainActivity: thread1 id :1601 after wait
10-27 15:35:39.592  6892  7055 I wp20170906-MainActivity: thread1 id :1601 after synchronized
10-27 15:35:39.592  6892  7055 I wp20170906-MainActivity: thread1 id :1601 end


10-27 15:35:40.704  6892  7060 I wp20170906-MainActivity: thread2 id :1604 end before synchronized
10-27 15:35:40.704  6892  7060 I wp20170906-MainActivity: thread2 id :1604 end before notify
10-27 15:35:40.705  6892  7060 I wp20170906-MainActivity: thread2 id :1604 end after notify
10-27 15:35:40.705  6892  7060 I wp20170906-MainActivity: thread2 id :1604 end after synchronized
10-27 15:35:40.705  6892  7059 I wp20170906-MainActivity: thread1 id :1603 after wait
10-27 15:35:40.705  6892  7059 I wp20170906-MainActivity: thread1 id :1603 after synchronized
10-27 15:35:40.705  6892  7059 I wp20170906-MainActivity: thread1 id :1603 end


10-27 15:35:42.598  6892  7063 I wp20170906-MainActivity: thread2 id :1606 end before synchronized
10-27 15:35:42.598  6892  7063 I wp20170906-MainActivity: thread2 id :1606 end before notify
10-27 15:35:42.598  6892  7063 I wp20170906-MainActivity: thread2 id :1606 end after notify
10-27 15:35:42.598  6892  7063 I wp20170906-MainActivity: thread2 id :1606 end after synchronized
10-27 15:35:42.599  6892  7062 I wp20170906-MainActivity: thread1 id :1605 after wait
10-27 15:35:42.599  6892  7062 I wp20170906-MainActivity: thread1 id :1605 after synchronized
10-27 15:35:42.599  6892  7062 I wp20170906-MainActivity: thread1 id :1605 end

wait传入特定时间:

new Thread(new Runnable() {    @Override    public void run() {        long id = Thread.currentThread().getId();        Log.i(TAG, "thread1 id :" + id + " begin");        new Thread(new Runnable() {            @Override            public void run() {                long id = Thread.currentThread().getId();                Log.i(TAG, "thread2 id :" + id + " begin");                try {                    Thread.sleep(4000);                } catch (InterruptedException e) {                    e.printStackTrace();                }                Log.i(TAG, "thread2 id :" + id + " end before synchronized");                synchronized (object){                    Log.i(TAG, "thread2 id :" + id + " end before notify");                    object.notify();                    Log.i(TAG, "thread2 id :" + id + " end after notify");                }                Log.i(TAG, "thread2 id :" + id + " end after synchronized");            }        }).start();        try {            Log.i(TAG, "thread1 id :" + id + " before synchronized");            synchronized (object){                Log.i(TAG, "thread1 id :" + id + " before wait");                object.wait(2000);                Log.i(TAG, "thread1 id :" + id + " after wait");            }            Log.i(TAG, "thread1 id :" + id + " after synchronized");        } catch (InterruptedException e) {            e.printStackTrace();        }        Log.i(TAG, "thread1 id :" + id + " end");    }}).start();

wait传入参数后
10-27 15:28:02.262  5907  6093 I wp20170906-MainActivity: thread1 id :1556 begin
10-27 15:28:02.264  5907  6093 I wp20170906-MainActivity: thread1 id :1556 before synchronized
10-27 15:28:02.264  5907  6093 I wp20170906-MainActivity: thread1 id :1556 before wait
10-27 15:28:02.266  5907  6094 I wp20170906-MainActivity: thread2 id :1557 begin
10-27 15:28:04.265  5907  6093 I wp20170906-MainActivity: thread1 id :1556 after wait
10-27 15:28:04.265  5907  6093 I wp20170906-MainActivity: thread1 id :1556 after synchronized
10-27 15:28:04.265  5907  6093 I wp20170906-MainActivity: thread1 id :1556 end
10-27 15:28:06.266  5907  6094 I wp20170906-MainActivity: thread2 id :1557 end before synchronized
10-27 15:28:06.266  5907  6094 I wp20170906-MainActivity: thread2 id :1557 end before notify
10-27 15:28:06.266  5907  6094 I wp20170906-MainActivity: thread2 id :1557 end after notify
10-27 15:28:06.266  5907  6094 I wp20170906-MainActivity: thread2 id :1557 end after synchronized