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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:26:38+00:00 2026-06-15T13:26:38+00:00

I read this stackoverflow topic, which conclusion seems to be that an empty synchronized

  • 0

I read this stackoverflow topic, which conclusion seems to be that an empty synchronized block can always be avoided with a nicer solution. The topic also has some unclear parts to me, which I’ll integrate to my below post.

Assume we have a class like this:

public class MyThreadClass extends Thread {
  private final OtherComponent mOtherComponent;
  private final Object mLock = new Object();
  private MyHandler mHandler;

  public MyCustomThread(OtherComponent otherComponent) {
      mOtherComponent = otherComponent;      

  public void run() {

      mHandler = new Handler() {...}

      // Other init operations

      mOtherComponent.onMyCustomThreadInitialized();

      // ... other operations (Looper.loop(), actually)
  }

  public void sendMessageWithHandler(int what, int arg) {
       synchronized (mLock) {}
       Message msg = mHandler.obtainMessage(what);
       msg.arg1 = arg;
       mHandler.sendMessage(msg);
  }

  public void useHandlerInAnotherWay(...) {
       synchronized (mLock) {
            // useful code that needs actual mutual exclusion
       }
       mHandler.sendMessage(...);
  }
}

The relevant part of my application works in the following way:

  1. The MyThreadClass thread is created and started.
  2. As an indirect consequence of mOtherComponent.onMyCustomThreadInitialized(), another part of my application will start spawning other threads. (Note that they aren’t started synchronously from this call, that is why I say it’s indirect consequence. The only point is, mHandler has been initialized by the time these other threads are started)
  3. Each of the other threads will call sendMessageWithHandler(...) exactly once
  4. Yet again other threads (i.e. not the threads mentioned above) call useHandlerInAnotherWay(...), this can happen at any time (after mOtherComponent.onMyCustomThreadInitialized(), of course).

My questions:

  1. If I’m correct, up-to-date data visibility must be guaranteed when mHandler is accessed from other threads than myThreadClass, because it is not a final field. I don’t want to make it volatile either, because except for these few sendMessageWithHandler(..) calls, mHandler is not used from other threads without synchronization (I don’t want the volatile overhead to be present unnecessarily where it’s needless). In other words, when mHandler is accessed from those yet other threads via useHandlerInAnotherWay(), the synchronized there with the “useful code” (i.e. code that actually needs to be a subject of mutual exclusion) also guarantees that the caller thread sees mHandler correctly. In sendMessageWithHandler(..), however, the code does not require mutual exclusion, so I decided to put an empty synchronized block to the beginning of sendMessageWithHandler(...). Is this correct? Is there a nicer solution to my problem?

  2. The other stackoverflow thread I linked to has the following answer (it’s not the accepted one, but was upvoted multiple times):

    It used to be the case that the specification implied certain memory
    barrier operations occurred. However, the spec has now changed and the
    original spec was never implemented correctly. It may be used to wait
    for another thread to release the lock, but coordinating that the
    other thread has already acquired the lock would be tricky.

    Does this mean that the empty synchronized does not offer the memory barrier functionality anymore? If I check the Java docs online about synchronized, they mention that ALL memory is updated due to it (i.e. thread copies are updated from “main memory” at monitor enter, and “main memory” is updated from thread copies at monitor exit). But they don’t mention anything about empty synchronized blocks, so this is unclear to me.

  • 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-15T13:26:39+00:00Added an answer on June 15, 2026 at 1:26 pm

    You probably don’t need any synchronization.

    (thread 1)          (thread 2)
    
    write
      |
    start thread 2
                     \
                        ...
                         |
                        read  
    

    read is guaranteed to see write.


    The semantics of synchronized is strictly enforced, even if it’s an empty block.

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

Sidebar

Related Questions

I've read many topic on stackoverflow,and google, about this argument but i can't orient
I've read every related topic that I can find on Stackoverflow and elsewhere and
I've read a lot on this topic already both here (e.g., stackoverflow.com/questions/1713554/threads-processes-vs-multithreading-multi-core-multiprocessor-how-they-are or multi-CPU,
I've read several stackoverflow posts about this topic, particularly this one: Secure hash and
I've read a couple similar questions regarding this topic on StackOverflow, but none of
I've been following this Stackoverflow topic . Everything works on the read side. I
From the sounds of this StackOverflow answer it seems that when I publish my
Now that I can make useful user controls in WPF (thanks to this stackoverflow
I read some questions about this topic here at stackoverflow but none of them
I read all topics related to this question in stackoverflow and whole internet and

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.