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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:07:27+00:00 2026-05-30T23:07:27+00:00

Lets say i have a thread running like this: private boolean working = true;

  • 0

Lets say i have a thread running like this:

private boolean working = true;

@Override public void run() {
   working = true;
   // do something
   working = false;
   ....
}

and in my main Thread i’m constantly putting out the state of working with

while(threadClassObject.isWorking()) {
    System.out.println(threadClassObject.isWorking());
}

would this work? I tried this example and it seems to work. But is there a way that this could crash? What e.g. happens if the thread is in the process of changing working while at the exact same time the mainThread tries to read it?

  • 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-30T23:07:29+00:00Added an answer on May 30, 2026 at 11:07 pm

    The ans to your question is that it might be working but the above code is a risky code and can break any day. Try making working volatile like

    private volatile boolean working = true;
    

    What e.g. happens if the thread is in the process of changing working while at the exact same time the mainThread tries to read it?

    Assignment operation is an atomic operation. So if you have single cpu, two threads can never collide while accessing the variable. If you have more than one cpu, two threads can collide while accessing the variable. For both cases, volatile will make sure that the value is visible to other threads.

    NOTE: volatile is good in your situation but if you have more complex data to share across thread try looking into this

    Edit:

    Adding the comment also part of the soln. to make it more clear.

    Basically bcoz of optimization at cpu level, values changed my one thread might not be visible to another. A good example is cpu cache bcoz optimization the values are never reflected in ram where the other thread might be reading. Volatile tells that this variable’s value can be changed outside the scope of current thread so no such optimization is done…

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

Sidebar

Related Questions

Lets say I have something like this: private CancellationTokenSource myToken; public void MyMyMethod() {
Let's say we have some code like this running in the separate thread: private
Lets say we have this code: bool KeepGoing = true; DataInThread = new Thread(new
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say I have an array like this: string [] Filelist = ... I
lets say I have a String like this [{ name : Ronald , firstname
I have a thread: Thread t = new Thread(){ public void run(){ ServerSocketConnection scn
Let's say I have one thread running (I'm creating many instances of this thread),
Lets say I have a class that is doing something like: public class Foo
Let's say I started a thread and I have something like this: ...//initiate all

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.