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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:03:06+00:00 2026-05-22T15:03:06+00:00

This is a section from Core Java 8th edition Page 757 CAUTION: public void

  • 0

This is a section from Core Java 8th edition Page 757

CAUTION:

public void flipDone() {
   done = !done;
}

// not atomic

I don’t understand why it’s not atomic. Can any one tell me why? 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-22T15:03:07+00:00Added an answer on May 22, 2026 at 3:03 pm

    The flipDone method is executed by the computer in three distinct steps:

    Read the value of memory location labeled done into ALU
    Flip the value (i.e true -> false or false -> true)
    Write the new value into the memory location done
    

    In Java, a piece of code can potentially be invoked in multiple threads. These threads should be thought of as executing the code concurrently.

    Say, memory location labeled done contains the value false initially. Consider two threads calling flipDone, resulting in the following sequence of steps:

    Thread 1                             Thread 2
    -----------------------------------------------------------------------
    Read value of done, false
    Invert that value to true
    Write the new value, true, to done       
                                         Read value of done, now true
                                         Invert that value to false
                                         Write the new value, false, to done
    

    The flipDone method was called twice. done went from false to true and then back again to false – as one would expect. But since the threads execute concurrently, this is not the only ordering of steps. Consider this ordering instead:

    Thread 1                             Thread 2
    -----------------------------------------------------------------------
    Read value of done, false
    Invert that value to true            Read value of done, false
    Write the new value, true, to done   Invert that value to true    
                                         Write the new value, true, to done
    

    While the first thread is inverting the value it read, the second thread, concurrently, is reading the value. Similarly, while the first thread is writing the value to memory, the second thread is inverting the value it read. When Thread 2 finishes, the value of done will be true. Here, although flipDone was called twice, done was flipped only once! One of the updates seem to have been lost. This is the problem that the book is trying to warn you about.

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

Sidebar

Related Questions

In this section of a function (.NET 2.0): public void AttachInput<T>(T input) where T
This is the code section from inno setup.My intention is to make two Checkbox
This is what I have typed in the footer message section from the site
In particular from this web.config: <configuration> <configSections> <section name=RStrace type=Microsoft.ReportingServices.Diagnostics.RSTraceSectionHandler,Microsoft.ReportingServices.Diagnostics /> </configSections> <system.diagnostics> <switches>
I am currently trying to make a sectioned table like this: Section 1: Entry
I'm getting a javascript error. It has to do with this section of code:
I've read this book section about git branches. I have create a branch called
I'm wondering why this code section prints out the following: print request.user.has_perm('bug_tracking.is_developer'): + str(request.user.has_perm('bug_tracking.is_developer'))
I have been trying to get this one section of my UI to immediatly
According to a section in this presumably accurate book , A common use of

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.