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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:42:13+00:00 2026-05-14T02:42:13+00:00

Consider the following Java code: volatile boolean v1 = false; volatile boolean v2 =

  • 0

Consider the following Java code:

volatile boolean v1 = false;
volatile boolean v2 = false;

//Thread A
v1 = true;
if (v2)
    System.out.println("v2 was true");

//Thread B
v2 = true;
if (v1)
    System.out.println("v1 was true");

If there was a globally visible total order for volatile accesses then at least one println would always be reached.

Is that actually guaranteed by the Java Standard? Or is an execution like this possible:

A: v1 = true;
B: v2 = true;
A: read v2 = false;
B: read v1 = false;
A: v2 = true becomes visible (after the if)
B: v1 = true becomes visible (after the if)

I could only find statements about accesses to the same volatile variable in the Standard (but I might be missing something).

“A write to a volatile variable (§8.3.1.4) v synchronizes-with all subsequent reads of v by any thread (where subsequent is defined according to the synchronization order).”

http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.4.4

Thanks!

  • 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-14T02:42:13+00:00Added an answer on May 14, 2026 at 2:42 am

    I think the quote answers your question more than you realize it does.

    The write to v2 by thread B in line #2 is “seen” by thread A when it attempts to read the variable.

    I’ve bolded the key part of the quote:

    “A write to a volatile variable (§8.3.1.4) v synchronizes with all subsequent reads of v by any thread …”

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

Sidebar

Related Questions

Consider the following classes in Java class A { protected void methodA() { System.out.println(methodA()
Consider the following Java code: String str = \u8ba9\u4ed6\u51fa\u6d77\u4e86; // ... System.out.print(str); I want
Let's consider the following code snippet in Java. There are some of possible approaches
Consider the following two blocks of Java psedo-code in a system that uses optimistic
Java noob question: Consider the following C array and initializer code: struct { int
I have a question about how GC works in Java. Consider the following code:
Consider the following Java code: Object a = new Integer(2); Object b = new
Let's consider the following code in Java. package obj; final class First { public
Consider the following two segments of code in Java, Integer x=new Integer(100); Integer y=x;
Let's consider the following code snippet in Java. package escape; final public class Main

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.