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?
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.
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.
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.
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.
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...
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With