Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8388765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:32:37+00:00 2026-06-09T18:32:37+00:00

synchronized does not appear to work as I had expected. Shouldn’t it make the

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:32:38+00:00Added an answer on June 9, 2026 at 6:32 pm

    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();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below does NOT work: Cause: I assume I tracked down the cause
Why synchronized keyword does not create monitor enter at byte code level every time
I'm not used to working with synchronized. Does the following snippet look right? public
How do atomic / volatile / synchronized work internally? What is the difference between
Anyidea why autocomplete does not work on the spaceScene property? #import <Foundation/Foundation.h> #import cocos2d.h
Does @synchronized not use lock and unlock to achieve mutual exclusion? How does it
I want to add a new calendar to Android that does not get synchronized.
Is it possible to make AsyncTask.doInBackground synchronized - or achieve the same result in
There is a JDK function that, although the javadocs does not declare it as
The onKeyDown method does not function. When i press the button the bitmap s

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.