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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:14:39+00:00 2026-06-13T22:14:39+00:00

static ConcurrentHashMap k; X x; //line 3: synchronized(k){ x = k.get(LL);} // line 5

  • 0
static ConcurrentHashMap k;

X x; 
//line 3: 
synchronized(k){ x = k.get("LL");}

// line 5 

// line 12: 
synchronized(k){if(x.b){x.b = false;}} 

‘k’ is a shared map. First thread goes through line 3, second thread goes through line 3 when thread 1 is at line 5, thread one alters x.b to false, what x.b does thread 2 see? Line 5 is meant to show that thread 2 gets its x before thread one got in the second synch block

  • 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-13T22:14:40+00:00Added an answer on June 13, 2026 at 10:14 pm

    You have somewhat overspecified the terms “first thread” and “second thread”; your question presupposes that the first thread to enter the first synchronized block will also be the first thread to enter the second synchronized block, but there’s really no reason to expect that.

    However, the first synchronized block doesn’t do anything very relevant or interesting — nothing in your code-snippet mutates k, and the first synchronized block merely accesses it — so I’m just going to ignore the fact that it’s synchronized. That will simplify the definitions slightly: now “first thread” means the first thread to enter the second synchronized block, and “second thread” means the second thread to enter the second synchronized block. (O.K. so far?) That matter of definition out of the way . . .

    Assuming that there’s no possibility of some other thread coming in and setting x.b to true — or, for that matter, of the first thread doing so, in code that’s after the snippet that you quote — and similarly, no possibility of the two threads getting completely different results for k.get("LL") due to things going on elsewhere — then the second thread will see x.b as false, just as one would naïvely expect. This is because

    If one action happens-before another, then the first is visible to and ordered before the second.

    and

    An unlock on a monitor happens-before every subsequent lock on that monitor.

    (Both of the above quotations are from §17.5.5 of The Java Language Specification, Java SE 7 Edition; see that section, and the section before it, for more formalities.)

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

Sidebar

Related Questions

static SerialPort port = new SerialPort(COM3, 57600, Parity.None, 8, StopBits.One); thread1() { lock(port) for(;;)
Given the following code: public class FooBar { public static volatile ConcurrentHashMap myConfigData =
static const map<const Type*, int>* priority; where Type is my own special class. Initialization
I'm trying to write a thread-safe Map[K, Set[V]] implementation in java. If a unique
I am working on a existing web based application which uses static map to
Static initializers are supposed to be executed once before the first reference to the
The question came up when I saw this code: private static volatile ConcurrentHashMap<String, String>
static void Job47(Args _args) { str path,stx; TreeNodeIterator iter; TreeNode treeNode, treeNodeToRelease; Map dictMenuDisplay;
This seems wrong. static ConcurrentHashMap k; //multiple threads have access to k X o
static <V> void myMethod(Map<?, V> map) { Iterator<Entry<?, V>> it = map.entrySet().iterator(); } I'm

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.