Java threading / volatile

I have a stream:

class Foo extends Thread { boolean active = true; public void run() { while(active) { //do stuff } } public void end() { active = false; } public void hibernate() { synchronized(this) { wait(); } } } 

If another thread calls end() , will Foo immediately see that active now false ? In particular, since active not volatile , I'm not sure if this will happen. I originally created end() as a smart way to avoid volatility, but now I'm not sure if it really does what I intend. Also, if another thread calls hibernate() , which thread will go into sleep mode? I intend Foo sleep, so if this does not do what I intend, an alternative suggestion would be very welcome.

+5
java multithreading volatile
Aug 27 2018-11-21T00:
source share
3 answers

If another thread calls end (), will Foo immediately see that false is now active?

No, it will not. Or at least he won’t see all this time.

If you want run always display the new value immediately, there must be a "come after" relationship between the thread assigned to the variable and the thread reading it. This can be achieved:

  • declaring active volatile,
  • by putting synchronized blocks around statements that read and write a variable,
  • making the variable an "atomic" type; e.g. AtomicBoolean , or
  • using another suitable concurrency class; see java.util.concurrent.* packages.

... a smart way to avoid volatiles ...

Declaring a variable to be volatile is one way to ensure proper synchronization. It is a fact that proper synchronization incurs overhead. However, proper synchronization is necessary for the reliable operation of your application, and is NOT smart to avoid it.

(Without proper synchronization, your program will probably work most of the time, and it may even work on some machines. However, sometimes this will not work, and the actual behavior will probably depend on which machine you run the program on, which load cars, etc.)

Also, if another thread calls hibernate (), which thread will sleep?

The thread causing the call will go into sleep mode. And it will not wake up if some other thread does not execute notify or notifyAll on the same Foo object.

If you just want the application to Thread.sleep bit, use Thread.sleep . But be careful that using sleep wrong way can make your application slow and unresponsive.

+12
Aug 27 '11 at 2:35 a.m.
source share

Your suspicion is true: since active not volatile , there is no guarantee that run() will ever see changes made in another thread.

Generally speaking, smart ways to avoid volatile are almost always a bad idea. In fact, even volatile is something you prefer not to resort to. In most cases, it is safer to stick to locks, monitors, or higher-level synchronization mechanisms.

For your second question, a thread that goes into sleep mode is called what is called hibernate() . This thread will sleep until it is interrupted, it experiences a false awakening or other calls to the notify() / notifyAll() on the monitor of the Foo instance. It is usually a mistake to call Object#wait() without surrounding it with a loop that checks for a condition that is waiting.

You also seem to be confused with the idea of ​​creating an instance of Foo "for sleep." An instance of Foo not Thread (or even a Runnable ) and does not create its own thread, so the idea of ​​sleeping it does not make much sense. You are probably trying to get the thread calling Foo#run() to sleep.

+5
Aug 27 2018-11-11T00:
source share

As for your first question on avoiding instability, try using the Thread interrupt to signal that the current thread has started.

Use the interrupt method () from another thread to interrupt a running thread.

Use isInterrupted () in your workflow to check for interruption.

 while(!this.isInterrupted()){ //do your work here. } 

Not sure why you want to extend the Thread class. If you implement Runnable, then you should use interrupted in your startup method to check for interruption. Read javadocs to learn about some of the caveats of this method.

0
Aug 27 '11 at 4:44
source share



All Articles