不同步的同步 [英] synchronized not synchronizing

查看:128
本文介绍了不同步的同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

同步不出现我预期工作。难道不应该使code原子相对于code的一个单独的块是同步括号内由同一个对象?

我发现它不同步我的code之内的。

 私有对象movementMutex_ =新的对象();//主题
公共无效的run()
{
    而(RUN_)
    {
        同步(movementMutex_)
        {
            如果(timeToMove_)
            {
                Log.v(咩,timeToMove_是真的,动);
                makeMove();
                Log.v(咩,移动完成设置timeToMove_为假。);
                timeToMove_ = FALSE;
                Log.v(咩,timeToMove_现在是假的);
            }
        }
    }
}
//通过不同的线程调用,所以此线程知道何时做出的举动
公共无效移动()
{
    Log.v(咩,等待中移动(movementMutex));
    //同步使timeToMove_没有得到移动中的中间设置为true,而因此pmaturely设置回假$ P $
    同步(movementMutex_)
    {
        Log.v(咩,招叫,设置timeToMove_);
        timeToMove_ = TRUE;
        Log.v(咩,timeToMove_现在是真正的);
    }
}

综观日志打印出来,我看到他们在一个意想不到的顺序打印。加粗的语句应该从我的理解同步,是uninter preTABLE由非粗体打印然而,这是不是这样的,我试图避免发生非常的事:我错过了下一步行动因为我把它真正的,当它还是已经成为事实,并及时把它错误的。

 十月8日至12日:47:19.860:V / MEH(27639):等待在移动movementMutex()
 10月8日至12日:47:19.985:V / MEH(27639):移动调用,设置timeToMove_
 10月8日至12日:47:19.985:V / MEH(27639):timeToMove_现在是真的
 10月8日至12日:47:19.985:V / MEH(27639):** timeToMove_是真的,动**
 10月8日至12日:47:20.352:V / MEH(27639):等待在移动movementMutex()

这下一行不应该是可能的。这不是等待movementMutex!

 十月8日至12日:47:20.352:V / MEH(27639):移动调用,设置timeToMove_
10月8日至12日:47:20.360:V / MEH(27639):timeToMove_现在是真的
10月8日至12日:47:20.360:V / MEH(27639):**移动完成。设置timeToMove_假**
10月8日至12日:47:20.360:V / MEH(27639):** timeToMove_现在是假**


解决方案

由于在评论中提到的问题,一个线程能够再进入其自身的同步锁。在makeMove()函数调用实际调用动(),这意味着它是在同一个线程执行之下,因此不会被锁定的始作俑者。总之,低于code不会导致死锁;它会执行没有问题,这就是我正在经历。

 同步(movementMutex_)
{
    同步(movementMutex_)
    {
        做一点事();
    }
}

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

解决方案

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

这篇关于不同步的同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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