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

  • Home
  • SEARCH
  • 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 9086529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:30:43+00:00 2026-06-16T21:30:43+00:00

I am an analog guy learning how to code on android so please bear

  • 0

I am an analog guy learning how to code on android so please bear with me if this question sounds stupid.

I understand the gist of synchronization. If i have data that two threads can access and modify it could lead to inconsistent values in the data variable in both threads.

It makes sense to make methods synchronized and lock them till one thread is done executing it (performing any conditional checks) but why would it matter with single statements? It would matter with multiple statements as theres a chance of thread one going out of runnable state in between 2 or more statements but i dont understand what the point is for a single statement.

Here is an example

if i have a listener that can be called from lets say a game thread and it modifies a data variable why would i do it this way…

    public void onCompletionListener(MediaPlayer player){
    synchronized (this){
        isPrepared = false;
       }
    }

i also have another method in my Music class called stop

   public void stop(){
   mediaPlayer.stop()
    synchronized(this){
    isPrepared = false;
       }
    }

Why would i bother to do this? Is this done for consistency of the value between the two threads? if so can i use something like volatile? would that be better?

I ask because i heard that synchronization is expensive. Are there better alternatives?

Thank you all in advance for answering my question. I appreciate it.
Edit: I read this synchronization on single statement?

I think i understand.. if someone could elaborate that would be awesome. Also part two of my initial question regarding whether volatile is a better alternative is unanswered 🙂

  • 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-16T21:30:44+00:00Added an answer on June 16, 2026 at 9:30 pm

    It is done because synchronization also puts in place requirements about memory visibility between threads.

    If that write is not synchronized or volatile, there aren’t any hard and fast requirements about when it becomes visible to other threads. Another thread could have a copy of isPrepared in its stack that says true, and it could run forever without knowing to go back to main memory and check if someone else has changed isPrepared if there’s no synchronization or volatile reference.

    The volatile keyword would satisfy the immediate requirement of making sure other threads see the update. It is impossible to say without seeing the entire class/system if it would be an acceptable replacement. It could be that there are other synchronized methods and it is illegal to change the state of isPrepared while one of them is mid-execution. Clearly it would then be an error to remove the synchronization in favour of a volatile reference.

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

Sidebar

Related Questions

What is analog in datamapper for .present? method? I have code from rails app
I have found this analog clock tutorial here: http://www.codeproject.com/KB/selection/AnalogClockControl.aspx Now is it possible to
Is there a pushMatrix()/popMatrix() analog to this processing code that tries to assign custom
Here is the code for an analog clock. Please check my code and help
perl hex() analog in python how to? I have next perl code: my $Lon
Is there a direct analog to this in C++? for num in [1,4,5] :
Does Eclipse have an analog to Visual Studio's Immediate Window, a window where I
Let's say I have a 12-bit Analog to Digital Converter (4096 bins). And let's
What is C# analog of C snprintf()? In C code here we use snprintf(buf,
What is C# analog of C fread()? In C code here we use fread(inbuf,

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.