Is my class thread safe? If not?

Is my class thread safe? If not?

class Foo { boolean b = false; void doSomething() throws Exception { while (b) Thread.sleep(); } void setB(boolean b) { this.b = b; } } 
+6
source share
5 answers

The code is not thread safe because the current thread can see the changes until the code is compiled (which may be at a random point later) and you no longer see the changes.

BTW: This makes testing very difficult. for example, if you sleep for 1 second, you may not see this behavior for almost three hours.

i.e. it may or may not work, and you cannot say only because it worked, it will continue to work.


Since b not volatile , JIT can and will optimize

 while (b) Thread.sleep(N); 

to be

 boolean b = this.b; if (b) while (true) Thread.sleep(N); 

therefore, the value of b not read every time.

+10
source

This is not true. setB() updates the instance variable b , but not synchronized .

Several threads may try to execute the setB() method at the same time, which can lead to unpredictable results.

You need a synchronize method (or) to use a synchronize block with a lock on the this object.

+7
source

Take a look at AtomicBoolean . This will mean that only one thread can access it at any given time.

+5
source

But how does this relate to thread safety? a little confused.

Return to the definition of thread safe. Wikipedia says this:

"Thread safety is a computer programming concept that is applicable in the context of multithreaded programs. A piece of code is thread safe if it works correctly when it is executed by multiple threads at the same time."

The key here is the correctness of the functions. If you look at the script described by Peter Laurie, you will see that compiling JIT can lead to code that does not notice when the value of b changes from true to false , and instead exits cyclically. This is clearly wrong behavior. And since this only appears when there are two threads, this is a thread safety issue.

+3
source

The concept of at-issue here is the visibility discussed in Concurrent Java. If doSomething () runs on a different thread, the JVM does not guarantee that the action taken in setB will apply to version b that doSomething does. That way you can call setB, and doSomething is not guaranteed to ever complete.

0
source

Source: https://habr.com/ru/post/926674/


All Articles