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 6081821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:13:08+00:00 2026-05-23T11:13:08+00:00

I am not fully understanding what the synchronization block is doing nor why it

  • 0

I am not fully understanding what the synchronization block is doing nor why it is necessary.
Can someone explain in a “synchronizing for dummies” kind of way?

In a book I am reading, the author tells me “The synchronization is necessary, since the members we manipulate within the
synchronized block could be manipulated in the onPause() method on the UI thread.”
He creates an Object named stateChanged and instantiates it as a new object.
Then, in the synchronization block he uses the stateChanged object as the argument.
This whole thing is throwing me off and I do not like to move on until I have a pretty good understanding of what is going on.

  • 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-05-23T11:13:09+00:00Added an answer on May 23, 2026 at 11:13 am

    The classic example is: Imagine you have two threads of operation, and both of them reference the same method:

    public void addToGlobalVar(int y) {
    int x = globalVar;  //what if a thread stops right after this line?
    x += y;
    globalVar = y;
    }
    

    where globalVar is some other predefined number that this method can interact with and set. Lets say globalVar is 50.

    Threads get computing time on a somewhat arbitrary basis, so you never fully know the precise nanosecond one stops and the other gets CPU time.

    In this example, if you launched an AsyncTask in addition to the UI thread, and both at some point use addToGlobalVar(10), what can happen is that one thread might be interrupted at line 2 of that code block. If the other thread goes through while that one is sleeping, it will successfully set globalVar to 60. But when the other one wakes up, it still thinks x = 50, and its going to then set it to 60. So in essence you just made 50+10+10 = 60. Hopefully you can see how this becomes a problem.

    You can fix this simple example by making the calculation atomic (skip declaring x, 1 line, all calcs done) or if the logic wasn’t able to be condensed to 1 line, you make a block of code atomic by using synchronized.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There's something wrong with my code or I'm just not understanding this fully. I
I am not fully understanding the idea of returning super.clone() in the clone() method
I'm not fully getting all i want from Google analytics so I'm making my
i do not fully understand how to communicate between adobe air (using flex3) and
Possible Duplicate: What are the reasons why Map.get(Object key) is not (fully) generic According
I've read Visual VM remotely over ssh but I think I've not fully understood
Being resonably new to using GTK+, im not fully aware of all its functionality.
I know Singleton pattern has been discussed so much. But because I'm not fully
Since uni, I've programming in Java for 3 years, although I am not fully
This would be my issue I have a drop down that's not displaying fully.

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.