Loop Doesn't See Value Changed by Other Thread Without a Print Statement

Loop doesn't see value changed by other thread without a print statement

The JVM is allowed to assume that other threads do not change the pizzaArrived variable during the loop. In other words, it can hoist the pizzaArrived == false test outside the loop, optimizing this:

while (pizzaArrived == false) {}

into this:

if (pizzaArrived == false) while (true) {}

which is an infinite loop.

To ensure that changes made by one thread are visible to other threads you must always add some synchronization between the threads. The simplest way to do this is to make the shared variable volatile:

volatile boolean pizzaArrived = false;

Making a variable volatile guarantees that different threads will see the effects of each other's changes to it. This prevents the JVM from caching the value of pizzaArrived or hoisting the test outside the loop. Instead, it must read the value of the real variable every time.

(More formally, volatile creates a happens-before relationship between accesses to the variable. This means that all other work a thread did before delivering the pizza is also visible to the thread receiving the pizza, even if those other changes are not to volatile variables.)

Synchronized methods are used principally to implement mutual exclusion (preventing two things happening at the same time), but they also have all the same side-effects that volatile has. Using them when reading and writing a variable is another way to make the changes visible to other threads:

class MyHouse {
boolean pizzaArrived = false;

void eatPizza() {
while (getPizzaArrived() == false) {}
System.out.println("That was delicious!");
}

synchronized boolean getPizzaArrived() {
return pizzaArrived;
}

synchronized void deliverPizza() {
pizzaArrived = true;
}
}

The effect of a print statement

System.out is a PrintStream object. The methods of PrintStream are synchronized like this:

public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}

The synchronization prevents pizzaArrived being cached during the loop. Strictly speaking, both threads must synchronize on the same object to guarantee that changes to the variable are visible. (For example, calling println after setting pizzaArrived and calling it again before reading pizzaArrived would be correct.) If only one thread synchronizes on a particular object, the JVM is allowed to ignore it. In practice, the JVM is not smart enough to prove that other threads won't call println after setting pizzaArrived, so it assumes that they might. Therefore, it cannot cache the variable during the loop if you call System.out.println. That's why loops like this work when they have a print statement, although it is not a correct fix.

Using System.out is not the only way to cause this effect, but it is the one people discover most often, when they are trying to debug why their loop doesn't work!


The bigger problem

while (pizzaArrived == false) {} is a busy-wait loop. That's bad! While it waits, it hogs the CPU, which slows down other applications, and increases the power usage, temperature, and fan speed of the system. Ideally, we would like the loop thread to sleep while it waits, so it does not hog the CPU.

Here are some ways to do that:

Using wait/notify

A low-level solution is to use the wait/notify methods of Object:

class MyHouse {
boolean pizzaArrived = false;

void eatPizza() {
synchronized (this) {
while (!pizzaArrived) {
try {
this.wait();
} catch (InterruptedException e) {}
}
}

System.out.println("That was delicious!");
}

void deliverPizza() {
synchronized (this) {
pizzaArrived = true;
this.notifyAll();
}
}
}

In this version of the code, the loop thread calls wait(), which puts the thread the sleep. It will not use any CPU cycles while sleeping. After the second thread sets the variable, it calls notifyAll() to wake up any/all threads which were waiting on that object. This is like having the pizza guy ring the doorbell, so you can sit down and rest while waiting, instead of standing awkwardly at the door.

When calling wait/notify on an object you must hold the synchronization lock of that object, which is what the above code does. You can use any object you like so long as both threads use the same object: here I used this (the instance of MyHouse). Usually, two threads would not be able to enter synchronized blocks of the same object simultaneously (which is part of the purpose of synchronization) but it works here because a thread temporarily releases the synchronization lock when it is inside the wait() method.

BlockingQueue

A BlockingQueue is used to implement producer-consumer queues. "Consumers" take items from the front of the queue, and "producers" push items on at the back. An example:

class MyHouse {
final BlockingQueue<Object> queue = new LinkedBlockingQueue<>();

void eatFood() throws InterruptedException {
// take next item from the queue (sleeps while waiting)
Object food = queue.take();
// and do something with it
System.out.println("Eating: " + food);
}

void deliverPizza() throws InterruptedException {
// in producer threads, we push items on to the queue.
// if there is space in the queue we can return immediately;
// the consumer thread(s) will get to it later
queue.put("A delicious pizza");
}
}

Note: The put and take methods of BlockingQueue can throw InterruptedExceptions, which are checked exceptions which must be handled. In the above code, for simplicity, the exceptions are rethrown. You might prefer to catch the exceptions in the methods and retry the put or take call to be sure it succeeds. Apart from that one point of ugliness, BlockingQueue is very easy to use.

No other synchronization is needed here because a BlockingQueue makes sure that everything threads did before putting items in the queue is visible to the threads taking those items out.

Executors

Executors are like ready-made BlockingQueues which execute tasks. Example:

// A "SingleThreadExecutor" has one work thread and an unlimited queue
ExecutorService executor = Executors.newSingleThreadExecutor();

Runnable eatPizza = () -> { System.out.println("Eating a delicious pizza"); };
Runnable cleanUp = () -> { System.out.println("Cleaning up the house"); };

// we submit tasks which will be executed on the work thread
executor.execute(eatPizza);
executor.execute(cleanUp);
// we continue immediately without needing to wait for the tasks to finish

For details see the doc for Executor, ExecutorService, and Executors.

Event handling

Looping while waiting for the user to click something in a UI is wrong. Instead, use the event handling features of the UI toolkit. In Swing, for example:

JLabel label = new JLabel();
JButton button = new JButton("Click me");
button.addActionListener((ActionEvent e) -> {
// This event listener is run when the button is clicked.
// We don't need to loop while waiting.
label.setText("Button was clicked");
});

Because the event handler runs on the event dispatch thread, doing long work in the event handler blocks other interaction with the UI until the work is finished. Slow operations can be started on a new thread, or dispatched to a waiting thread using one of the above techniques (wait/notify, a BlockingQueue, or Executor). You can also use a SwingWorker, which is designed exactly for this, and automatically supplies a background worker thread:

JLabel label = new JLabel();
JButton button = new JButton("Calculate answer");

// Add a click listener for the button
button.addActionListener((ActionEvent e) -> {

// Defines MyWorker as a SwingWorker whose result type is String:
class MyWorker extends SwingWorker<String,Void> {
@Override
public String doInBackground() throws Exception {
// This method is called on a background thread.
// You can do long work here without blocking the UI.
// This is just an example:
Thread.sleep(5000);
return "Answer is 42";
}

@Override
protected void done() {
// This method is called on the Swing thread once the work is done
String result;
try {
result = get();
} catch (Exception e) {
throw new RuntimeException(e);
}
label.setText(result); // will display "Answer is 42"
}
}

// Start the worker
new MyWorker().execute();
});

Timers

To perform periodic actions, you can use a java.util.Timer. It is easier to use than writing your own timing loop, and easier to start and stop. This demo prints the current time once per second:

Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
System.out.println(System.currentTimeMillis());
}
};
timer.scheduleAtFixedRate(task, 0, 1000);

Each java.util.Timer has its own background thread which is used to execute its scheduled TimerTasks. Naturally, the thread sleeps between tasks, so it does not hog the CPU.

In Swing code, there is also a javax.swing.Timer, which is similar, but it executes the listener on the Swing thread, so you can safely interact with Swing components without needing to manually switch threads:

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Timer timer = new Timer(1000, (ActionEvent e) -> {
frame.setTitle(String.valueOf(System.currentTimeMillis()));
});
timer.setRepeats(true);
timer.start();
frame.setVisible(true);

Other ways

If you are writing multithreaded code, it is worth exploring the classes in these packages to see what is available:

  • java.util.concurrent
  • java.util.concurrent.atomic
  • java.util.concurrent.locks

And also see the Concurrency section of the Java tutorials. Multithreading is complicated, but there is lots of help available!

Java Thread doesn't stop While Loop unless I print Something Inside

Yes, it isn't actually checking the variable.
The conpiler optinizes the loop and keeps the variable in a register. Putting some content in the loop changed that behavior.

You can try marking the variable as volotile as you should for any variable shared between threads.

However a busy loop on a shared variable is almost definetly not the way for one thread to await a signal from another. You want to use a lock and not use up cpu with a pointless loop.

In java the simplest form is to create a lock object and use wait/notify on it.

Error accessing the Cache in the Application_Start method of Global.asax

Wow. I found the answer to this. Use the variable HttpRuntime.Cache instead of Context.Cache.

Value does not update in while loop unless printed out

If the only thing changing between your working and non working code is the println statement, then you almost certainly have a threading issue. The System.out.println() statement adds a small pause which may coincidentally cause it to behave, but is not solving the issue. You could do something like:

try {
Thread.sleep(10);
} catch (InterruptedException ex) {
}

...in place of this and check for the same behaviour, if indeed it is the same then this pretty much confirms the threading issue. However, as pointed out below println() also does a few other things such as causing a memory barrier, so this isn't a foolproof test. Another, perhaps better check could be to temporarily swap out ArrayList for Vector, which is thread safe - though this is a legacy collection so not recommended for use in the final code.

If this is the case, it sounds like you're not synchronising on ArrayList calls properly - ArrayList is not a thread safe collection. Whenever you read or write to the list, do it inside a synchronized block like so, synchronizing on the list:

synchronized(list) {
list.whatever();
}

...which will ensure that only one thread can access the ArrayList at once, hopefully solving the threading issue.

Java while loop can not detect change by thread

This is because static int num is not marked volatile. Java "knows" that num is not going to change after the first read, so it is not reading it again.

Change the declaration to

static volatile int num;

to fix this problem. This will force memory reads on each access to num.

Note: static variables are not a good way of communicating between threads.

Adding a print statement to a loop working in a thread making it run faster

I fixed this by changing the order of time.sleep. The issue was, since message["substep"]= "skip" most of the time, therefore, time.sleep wouldn't execute and the thread doesn't get the chance to sleep. Therefore it would keep running very fast, without allowing other threads to breath some.

class PubSub(QRunnable):
def __init__(self):
QRunnable.__init__(self)
def run(self):
while True:
message["substep"] = "xyz"
message["timestamp"] = str(datetime.now().replace(microsecond=0))
time.sleep(1)
if message["substep"] == "skip":
continue
messageJson = json.dumps(message)
myAWSIoTMQTTClient.publish(self.topic, messageJson, 1)

The reason why print statement would help other threads to run faster previosuly is because I/O operations are slow and therefore would block this current thread for a while and would allow other threads to continue their work.



Related Topics



Leave a reply



Submit