synchronized not synchronizing

六眼飞鱼酱① 提交于 2019-12-10 11:28:18

问题


synchronized does not appear to work as I had expected. Shouldn't it make the code within the brackets atomic with respect to a separate block of code that is synchronized by the same object?

I'm finding that it's not synchronizing at all within my code.

private Object movementMutex_ = new Object();    

// Thread
public void run()
{
    while (run_)
    {
        synchronized(movementMutex_)
        {
            if (timeToMove_)
            {
                Log.v("meh", "timeToMove_ was true, moving");
                makeMove();
                Log.v("meh", "Move Complete. Setting timeToMove_ to false");
                timeToMove_ = false;
                Log.v("meh", "timeToMove_ is now false");
            }
        }
    }
}


// Called by a different thread so that this thread knows when to make a move
public void move()
{
    Log.v("meh", "awaiting movementMutex in move()");
    // Synchronizing so that timeToMove_ doesn't get set true while in the middle of moving and thus setting it back false prematurely
    synchronized(movementMutex_)
    {
        Log.v("meh", "move called, setting timeToMove_");
        timeToMove_ = true;
        Log.v("meh", "timeToMove_ is now true");
    }
}

Looking at the Log printouts, I'm seeing them print in an unexpected order. The statements in bold should, from my understanding of synchronized, be uninterpretable by the non-bold printouts yet this is not the case and the very thing I'm attempting to avoid is occurring: I'm missing the next move because I set it true when it was still already true and promptly turned it false.

 08-12 10:47:19.860: V/meh(27639): awaiting movementMutex in move()  
 08-12 10:47:19.985: V/meh(27639): move called, setting timeToMove_  
 08-12 10:47:19.985: V/meh(27639): timeToMove_ is now true  
 08-12 10:47:19.985: V/meh(27639): **timeToMove_ was true, moving**  
 08-12 10:47:20.352: V/meh(27639): awaiting movementMutex in move()  

This next line should not be possible. It's not awaiting the movementMutex!

08-12 10:47:20.352: V/meh(27639): move called, setting timeToMove_  
08-12 10:47:20.360: V/meh(27639): timeToMove_ is now true  
08-12 10:47:20.360: V/meh(27639): **Move Complete. Setting timeToMove_ to false**  
08-12 10:47:20.360: V/meh(27639): **timeToMove_ is now false**  

回答1:


As mentioned in the comments to the question, a thread is capable of reentering its own synchronized lock. The "makeMove()" function call was actually the instigator of calling "move()" which meant it was executing under the same thread and thus not being locked out. In short, the below code would not cause a deadlock; it would execute without a problem and this is what I was experiencing.

synchronized(movementMutex_)
{
    synchronized(movementMutex_)
    {
        doSomething();
    }
}


来源:https://stackoverflow.com/questions/11923296/synchronized-not-synchronizing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!