每次更改synchronized关键字的位置时,wait(n)的行为都不同 [英] wait(n) is acting different each time I change the position of synchronized keyword

查看:149
本文介绍了每次更改synchronized关键字的位置时,wait(n)的行为都不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考下面的代码

     public void acquire(){
        synchronized(a){
            print("acquire()");
            try{
                //Thread.sleep(5000);
                synchronized(this){
                    wait(5000);
                }
                print("I have awoken");
                print("" + a);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        print("Leaving acquire()");
    }


    public void modify(int n){
        print("Entered in modfy");
        synchronized(a){
            try{
                //Thread.sleep(5000);
                synchronized(this){
                    wait(5000);
                }
                this.a=n;
                print("new value" + a);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

        final SynchoTest ttObj = new SynchoTest();
        Thread A = new Thread(new Runnable(){
            public void run() {
                ttObj.acquire();    
            }
        },"A");

        Thread B = new Thread(new Runnable(){
            public void run() {
                ttObj.modify(97);   
            }
        },"B");
        A.start();
        B.start();

我知道wait(n),它会暂停一个线程,直到notify()/ notifyAll()被叫或指定时间n结束。

As i know about wait(n), it pauses a thread until notify()/notifyAll() get called or the specified time n is over.

但是......


  • 在上面的方法中,如果我直接使用wait(n),因为我使用Thread.sleep(n),
    我得到运行时异常。

  • 如果我同步两种方法而不是周围使用
    synchronized块等待(n)然后我没有得到任何异常但是
    线程都被永远阻止。

  • 但是如果我喜欢我附加即等待( n)周围有同步的
    块,它工作正常。

请告诉我原因?还告诉我为什么如果我使用sleep(n)而不是wait(n),它在定位同步块上的行为不同?

Please tell me why? Also tell me why is it not behaving different on positioning synchronized block if I use sleep(n) instead of wait(n)?

我的问题是关于等待的各种结果(n)关于synchronized关键字的各种位置。

My question is about various result of wait(n) on various position of synchronized keyword.

@Gray


通知()或notifyAll(),并且wait()必须在同步块
中等待您正在等待的对象

notify() or notifyAll(), and wait() must be in a synchronized block for the object you are waiting on

解释了为什么我通过在各个位置定位同步块来获得运行时异常。

explained me why I was getting run time exception by positioning synchronized block on various position.

现在请解释我,为什么

public void method(){
    synchronized(a){
        synchronized(this){
            wait(n);
        }
    }
}

工作正常。但

public synchronized void method(){
    synchronized(a){
        wait(n);
    }
}

永远阻止我的线程。

推荐答案

这与定位synchronized关键字无关。您遇到问题,因为您锁定其他对象并尝试等待另一个对象。好吧@Gray已经解释过了,所以不要重复它。

It is nothing about positioning synchronized keyword. You are facing problem since you locking other object and try to wait for another. Well @Gray has already been explained it, so not repeating it.

关于你的另一个问题,关于为什么两个线程都被阻止;

For your another problem, regarding why both threads are getting blocked;


线程A:锁定此[A:Runnable]

Thread A: locks this [A: Runnable]

线程A:锁定[A:Runnable]

Thread A: locks a [A: Runnable]

线程B:等待这个[A:Runnable,B:BLOCKED]

Thread B: waiting for this [A: Runnable, B:BLOCKED]

线程A:释放这个(等待)[A:定时等待,B:阻塞]

Thread A: release this (meets wait) [A: TIMED WAITING, B:BLOCKED]

线程B:锁定[A:定时等待,B:可运行]

Thread B: lock this [A: TIMED WAITING, B: Runnable]

线程B:等待线程A锁定的线程A [A:定时等待,B:阻塞]

Thread B: waiting for a which is already locked by thread A [A: TIMED WAITING, B:BLOCKED]

线程A:等待由线程B锁定的[A:阻塞,B:阻塞]

Thread A: waiting for this which is locked by thread B [A: BLOCKED, B:BLOCKED]

这篇关于每次更改synchronized关键字的位置时,wait(n)的行为都不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆