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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:20:43+00:00 2026-05-19T13:20:43+00:00

I am refering to page 261 – 262 of Joshua Bloch Effective Java //

  • 0

I am refering to page 261 – 262 of Joshua Bloch Effective Java

// Properly synchronized cooperative thread termination
public class StopThread {
    private static boolean stopRequested;

    private static synchronized void requestStop() {
        stopRequested = true;
    }

    private static synchronized boolean stopRequested() {
        return stopRequested;
    }

    public static void main(String[] args) throws InterruptedException {
        Thread backgroundThread = new Thread(new Runnable() {
            public void run() {
                int i = 0;
                while (!stopRequested())
                    i++;
            }
        });
        backgroundThread.start();
        TimeUnit.SECONDS.sleep(1);
        requestStop();
    }
}

Note that both the write method
(requestStop) and the read method
(stop- Requested) are synchronized. It
is not sufficient to synchronize only
the write method! In fact,
synchronization has no effect unless
both read and write operations are
synchronized.

Joshua’s example is synchronized on this. However My doubt is that, must synchronized be acted on the same object? Say, if I change the code to

    private static void requestStop() {
        synchronized(other_static_final_object_monitor) {
            stopRequested = true;
        }
    }

    private static synchronized boolean stopRequested() {
        return stopRequested;
    }

will this still able to avoid liveness failure?

That’s is, we know grabbing monitor for a same object during read/write can avoid liveness failure (According to Joshua Bloch’s example). But how about grabbing monitor for different object during read/write?

  • 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-19T13:20:44+00:00Added an answer on May 19, 2026 at 1:20 pm

    I don’t believe it’s guaranteed, although I wouldn’t be surprised if it actually was okay in all existing implementations. The Java Language Specification, section 17.4.4 states this:

    An unlock action on monitor m synchronizes-with all subsequent lock actions on m (where subsequent is defined according to the synchronization order).

    I believe that all the safety of reading/writing shared variables within locks stems from that bullet point in the spec – and that only specifies anything about a lock and an unlock action on a single monitor.

    EDIT: Even if this did work for a single variable, you wouldn’t want to use it for multiple variables. If you update multiple variables while holding a monitor and only read from them when holding a monitor, you can ensure that you always read a consistent set of data: nothing’s going to write to variable Y before you’ve read that but after you’ve read variable X. If you use different monitors for reading and writing, that consistency goes away: the values could be changed at any time while you’re reading them.

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

Sidebar

Related Questions

I get this, clicking 'no' means the page displays flawlessly. it's refering to this
provisioning problem again. Refering to my question: iPhone Development Provisioning Assistant Step 3 public/private
I am using javascript to get the referring page (document.referrer) which works fine and
Referring to this question , how can i get the current page size in
Refering to a previously asked question , I would like to know how to
I am refering to the question on changing the classpath programmatically. I read and
I have tried to do this refering to following link. http://skypher.com/index.php/2008/07/28/function-list-for-php/ But no success.
I am using Server version: Apache/1.3.34 (Debian) mod_perl - 1.29 By refering to STDIN,
I was watching MIX session. Presenter there was refering Panoramic experience in windows phone.
Using two ScriptManagers(ASP.Net and Telerik) will effect the page performance ? I have both

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.