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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:13:22+00:00 2026-06-17T14:13:22+00:00

I read FutureTask class in jsr166, found that outcome object is non-volatile, the comments

  • 0

I read FutureTask class in jsr166, found that outcome object is non-volatile, the comments in code is “non-volatile, protected by state reads/writes” line 75, the state is volatile int. I have read Java Memory Model from Java Language Spec, but not found the accurate answer. Does anybody know the reason?

  • 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-17T14:13:23+00:00Added an answer on June 17, 2026 at 2:13 pm

    consider this program:

    volatile int state;  
    
    Integer   result;
    
    void succeed(Integer result)
        if(state==PENDING)              vr0
            this.result = result;        w1
            state = DONE;               vw1
    
    Integer peekResult()
        if(state==DONE)                 vr2 
            return result;               r2
        return null;
    

    If volatile read vr2 sees DONE, it means it happens after volatile write vw1. So we have happens-before relationships: w1 -> vw1 -> vr2 -> r2. Therefore write w1 is visible to read r2.

    However succeed() isn’t thread safe, since vr0 and vw1 are not atomic. If we use CAS

    void succeed(Integer result)
        if( compareAndSet(state, PENDING, DONE) )      vr0+vw0
            this.result = result;                       w1
    

    it fixes the atomicity issue. However, now w1 isn’t necessarily visible to r2. The memory barrier effect of CAS is kind of like

    void succeed(Integer result)
        if(state==PENDING)         vr0
            state=DONE;            vw0
            this.result = result;   w1
    

    We have here vw0 -> vr2 -> r2, but w1 is not on the chain, there is no w1 -> r2

    We must do the volatile write state=DONE after w1 to establish the happens-before chain.

    void succeed(Integer result)
        if(state==PENDING)            vr0
            state=TMP;                vw0
            this.result = result;      w1
            state=DONE;               vw1
    

    or in CAS

    void succeed(Integer result)
        if( compareAndSet(state, PENDING, TMP) )       vr0+vw0
            this.result = result;                       w1
            state=DONE;                                vw1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Read the comments in the code for a description: <?php class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
Read that the following code is an example of unsafe construction as it allows
I read in a particular site that the code: txtBox = lstBox.ItemData(lstBox.ListIndex()) will provide
Read the edit below for more information. I have some code below that I
Read Carefully The question here is not that does PHP parser processes comments or
I read about the LinkedIn API and found out that there's no way we
Read this question today about safe and unsafe code I then read about it
I read Jeff's blog post that someone had managed to see a closed source
read the following code: // Creiamo la toolbar sotto UIToolbar *toolbar = [[UIToolbar alloc]
Read through this code. I wrote it at my desk in 5 minutes, it's

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.