Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting value of a thread variable from outside

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?

like image 462
Biene Maja Avatar asked Mar 04 '12 01:03

Biene Maja


People also ask

Can we return any value from thread?

A thread cannot return values directly. The start() method on a thread calls the run() method of the thread that executes our code in a new thread of execution.

Can two threads access same variable?

Only one thread can read and write a shared variable at a time. When one thread is accessing a shared variable, other threads should wait until the first thread is done. This guarantees that the access to a shared variable is Atomic, and multiple threads do not interfere.

Which method provides entry point for a thread?

While creating a thread class we must override the run() method of the Thread class. This method provides an entry point for the thread and you will put your complete business logic inside this method.

Do threads share instance variables?

Given the architecture of the JVM, you need only be concerned with instance and class variables when you worry about thread safety. Because all threads share the same heap, and the heap is where all instance variables are stored, multiple threads can attempt to use the same object's instance variables concurrently.


2 Answers

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...

like image 130
havexz Avatar answered Sep 19 '22 10:09

havexz


You might also want look into doing something with your "runaway" while loop in your main thread:

The current implementation will keep spinning, not leaving much CPU time left for your threads executing run() (or anyone else in the system, for that matter).

like image 42
theartofrain Avatar answered Sep 20 '22 10:09

theartofrain