wait和notify

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值