2

synchronized似乎没有像我预期的那样工作。它不应该使括号内的代码相对于synchronized同一对象的单独代码块具有原子性吗?

我发现它在我的代码中根本没有同步。

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");
    }
}

查看日志打印输出,我看到它们以意外的顺序打印。根据我对同步的理解,粗体的语句应该不能被非粗体打印输出解释,但事实并非如此,而我正试图避免的事情正在发生:我错过了下一步,因为我设置了它当它仍然是真实的时候是真实的,并迅速将其变为虚假。

 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()  

下一行应该是不可能的。它不是在等待运动互斥!

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**  
4

1 回答 1

1

正如对问题的评论中提到的,线程能够重新进入自己的同步锁。“makeMove()”函数调用实际上是调用“move()”的发起者,这意味着它在同一个线程下执行,因此不会被锁定。简而言之,下面的代码不会导致死锁;它可以毫无问题地执行,这就是我所经历的。

synchronized(movementMutex_)
{
    synchronized(movementMutex_)
    {
        doSomething();
    }
}
于 2012-08-14T16:35:24.533 回答