# While loop bug? Stuck on empty

**URL:** <https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557>\
**Category:** Coding Questions\
**Created:** [August 11, 2018, 2:15pm UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557 "2018-08-11T14:15:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamtomodea](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/iamtomodea/32/1054_2.png) [@iamtomodea](https://discourse.processing.org/u/iamtomodea)\
**Post date:** [August 11, 2018, 2:15pm UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/1 "2018-08-11T14:15:52Z")

</div>

Hi

I have a weird bug in a sketch that I’m using to control a plotter.  
I have a while loop that is controlled by a boolean that is switched to false while the plotter is in the middle of doing something. Once the plotter is finished it sends a serial command to my sketch and flips `nextMove=true`;  
This aspect of it is working correctly and I am checking the nextMove variable and it is set to true.

The weird part the sketch gets stuck in the loop even though the variable is set to true. If I put a prinlnt statement (as below) into the loop it doesn’t get stuck.  
I don’t want to print endlessly to the console as I cant read my other debug messages.

Any ideas? I tried to give the loop another meaningless task incrementing a variable but it still gets stuck. The print statement is all that will move it along.

Thanks

```auto
    nextMove=false;  
    boolean printOnce = true; 

while (nextMove == false) { // wait for plotter not to be busy
      if(printOnce)println("Waiting for plotter to move home"); //print busy message
      //println("Stuck in while loop");
      printOnce = false;
    }

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [August 11, 2018, 3:30pm UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/2 "2018-08-11T15:30:03Z")

</div>

There is nothing inside the while loop to change `nextMove` to true so if it is false when we reach the loop then program execution it stays indie the loop. The only way out is if it is changed in another thread.

---

<div class="post-metadata">

**Author:** ![iamtomodea](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/iamtomodea/32/1054_2.png) [@iamtomodea](https://discourse.processing.org/u/iamtomodea)\
**Post date:** [August 11, 2018, 3:33pm UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/3 "2018-08-11T15:33:49Z")

</div>

Hi,

Yeas, nextMove is changed in another thread that is listening for serial feedback from the plotter.  
As I mentioned the loop functions as intended if the print statement is uncommented but if the statement is commented out it gets stuck. Which seems a bit odd to me.

Does that make sense?

On another note - I realised I was setting needlessly printOnce each time so I moved it into the if statement as below.

```auto
    nextMove=false; // tell loop plotter is busy
    boolean printOnce = true; // control for busy message
    while (nextMove == false) { // wait for plotter not to be busy
      if(printOnce){
        println("Waiting for plotter to move home"); //print busy message
        printOnce = false;
      }
      println("Stuck in while loop");
      
    }

```

---

<div class="post-metadata">

**Author:** ![iamtomodea](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/iamtomodea/32/1054_2.png) [@iamtomodea](https://discourse.processing.org/u/iamtomodea)\
**Post date:** [August 11, 2018, 3:34pm UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/4 "2018-08-11T15:34:47Z")

</div>

Just to clarify - the purpose of the while loop is to wait for the nextMove command to be set true again based on external input from the plotter.

Thanks

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [August 11, 2018, 5:55pm UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/5 "2018-08-11T17:55:28Z")

</div>

It could be that with the print statements commented out the while-loop is hogging the CPU. I suggest that you put the thread to sleep for about 20 milliseconds inside the loop so that the other threads are not starved of CPU time.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 11, 2018, 6:23pm UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/6 "2018-08-11T18:23:32Z")

</div>

- Is your `while ()` loop under the “Animation” Thread?
- If it is so, you’re better off calling **noLoop()** in place of that loop.
- And have your other Thread calling **loop()** or **redraw()** instead of `nextMove = true;`.

---

<div class="post-metadata">

**Author:** ![iamtomodea](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/iamtomodea/32/1054_2.png) [@iamtomodea](https://discourse.processing.org/u/iamtomodea)\
**Post date:** [August 12, 2018, 2:58pm UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/7 "2018-08-12T14:58:28Z")

</div>

Hi Thanks for the help.

@GoToLoop yeah at the moment the the function that is talking to the plotter is called from the main draw thread. I should put this in another thread I guess? I don’t want to call noLoop() as I want an animation to still run on screen while the plotter is drawing the image. (Although at the moment of course this is not working as the draw() function needs to wait until the plotter has done its thing. So I will need to thread this anyway.

@quark Okay, good idea. Is Thread.sleep(20) the right way to go about this? this seems to throw an interupt exception.

Thanks  
T

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 12, 2018, 3:49pm UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/8 "2018-08-12T15:49:03Z")

</div>

> [@iamtomodea](#):
>
> Is `Thread.sleep(20)` the right way to go about this?

Just use **delay()**: [delay() / Reference / Processing.org](http://Processing.org/reference/delay_.html)  
But make sure it’s outside the “Animation” Thread. ⚠

---

<div class="post-metadata">

**Author:** ![iamtomodea](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/iamtomodea/32/1054_2.png) [@iamtomodea](https://discourse.processing.org/u/iamtomodea)\
**Post date:** [August 12, 2018, 4:07pm UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/9 "2018-08-12T16:07:43Z")

</div>

HI,

Great. I rejigged things and put the plotting function in another thread - so that side of it is sorted.  
I also swapped the println for a delay - it seems to work sometimes and then stop other times. I presume that what is happening is the serial command is coming back to it during the delay or something like that.  
I guess its the classic fix one bug and theres another few waiting in store.  
Thanks for the help.

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [August 12, 2018, 4:48pm UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/10 "2018-08-12T16:48:57Z")

</div>

> [@iamtomodea](#):
>
> nextMove is changed in another thread that is listening for serial feedback from the plotter

If doing this you should make sure the boolean field is also marked volatile. The other thread is effectively allowed to ignore the fact that it’s changed otherwise, particularly a problem in a hard loop! The JIT compiler can end up compiling your code such that the check happens once _before_ your loop. The print is probably changing this behaviour.

---

<div class="post-metadata">

**Author:** ![iamtomodea](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/iamtomodea/32/1054_2.png) [@iamtomodea](https://discourse.processing.org/u/iamtomodea)\
**Post date:** [August 12, 2018, 5:09pm UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/11 "2018-08-12T17:09:47Z")

</div>

HI. Ah cool. I didn’t know about that. So just declare it as volatile boolean.

Just so I understand it better - what’s the difference between a volatile boolean and a regular one? Is it that it can be read and written from different threads so that the thread in question has to check its value each time its references instead of assuming it is the same as it last set it?

thanks for the help, I don’t have much coding experience so I get a bit lost when I stray off the paths that I am familiar with.

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [August 12, 2018, 5:19pm UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/12 "2018-08-12T17:19:01Z")

</div>

> [@iamtomodea](#):
>
> the thread in question has to check its value each time its references instead of assuming it is the same as it last set it?

That’s close enough! You could have a read of [Java memory model - Wikipedia](https://en.m.wikipedia.org/wiki/Java_memory_model) for a bit more on caching / as-if-serial / reordering.

---

<div class="post-metadata">

**Author:** ![iamtomodea](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/iamtomodea/32/1054_2.png) [@iamtomodea](https://discourse.processing.org/u/iamtomodea)\
**Post date:** [August 12, 2018, 5:54pm UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/13 "2018-08-12T17:54:26Z")

</div>

Great, thanks for the heads up. I’ll have a look and see whether it works when I get back into the studio.  
Thanks

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 13, 2018, 2:55am UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/14 "2018-08-13T02:55:34Z")

</div>

> [@neilcsmith](#):
>
> The other Thread is effectively allowed to ignore the fact that it’s changed otherwise, particularly a problem in a hard loop!

Only for extreme tight loops like `while (!nextMove) { }`, w/o anything to it, would cause JIT to never check whether the non-`volatile` field _nextMove_ had changed its value.

For any healthy regular loops, JIT will eventually check non-`volatile` fields, and break outta the loop!

If it wasn’t so, the whole Java ecosystem would crumble in an instant due to your non-`volatile` fields doomsday multi-threading scenario!

We can confidently use a loop to await for any non-`volatile` field to be changed from another Thread!

Here’s a practical example, which awaits for Capture::_width_ to have non-zero value in a `while ()` loop under the sketch’s own “Animation” Thread:

```auto
/**
 * Efficient WebCam Capture (2.0.1)
 * GoToLoop (2016-May-05)
 *
 * Forum.Processing.org/two/discussion/16435/
 * how-to-make-the-webcam-capture-window-go-away-or-put-images-over-it
 *
 * GitHub.com/processing/processing-video/pull/30
 */

import processing.video.Capture;
Capture cam;

static final String RENDERER = JAVA2D;
//static final String RENDERER = FX2D;

static final int CAM = 1, FPS_ADJUST = 5, DELAY = 5;

void setup() {
  size(640, 480, RENDERER);
  initFeed();

  float canvasFPS = cam.frameRate + FPS_ADJUST;
  frameRate(canvasFPS);

  println("Cam's FPS:", cam.frameRate, "\t\tCanvas's FPS:", canvasFPS);
  print("Cam's size:", cam.width, 'x', cam.height, '\t');
  println("Canvas's size:", width, 'x', height);
}

void draw() {
  background(cam);
  getSurface().setTitle( str(round(frameRate)) );
}

void captureEvent(final Capture c) {
  c.read();
}

void initFeed() {
  String[] cams = Capture.list();
  printArray(cams);
  println("\nChosen Cam #" + CAM + ':', cams[CAM]);

  ( cam = new Capture(this, cams[CAM]) ).start();

  println("cam.width: " + cam.width); // cam.width: 0
  while (cam.width == 0) delay(DELAY);
  println("cam.width: " + cam.width); // cam.width: 640

  if (cam.width > 0) getSurface().setSize(cam.width, cam.height);
}

```

Pay attention to this particular code block:

```auto
println("cam.width: " + cam.width); // cam.width: 0
while (cam.width == 0) delay(DELAY);
println("cam.width: " + cam.width); // cam.width: 640

```

Before `while (cam.width == 0) delay(DELAY);` starts, **println()** is used to log the current value of Capture::_width_ non-`volatile` field, which is still `0`.

Of course that loop, which is run by the “Animation” Thread btW, expects Capture’s own Thread to change its non-`volatile` field _width_.

Even though the “Animation” Thread coulda cached the non-`volatile` field Capture::_width_, it will eventually check whether the real Capture::_width_ has changed its value.

According to your scenario, the “Animation” Thread would never be aware of the changes made to Capture::_width_ by Capture’s own Thread!

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [August 13, 2018, 9:03am UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/15 "2018-08-13T09:03:01Z")

</div>

> [@GoToLoop](#):
>
> We can confidently use a loop to await for any non- `volatile` field to be changed from another Thread!

I have no desire to go over the same argument with you again @GoToLoop No vaguely competent Java programmer is going to agree with you. The JMM exists for a reason - working outside of it you cannot be _confident_ of anything. You may have code that works in one scenario, on one JVM, on one OS or CPU (hardware behaviour very relevant here too) - that does not mean you can be confident in it. Your camera example is a particularly useless example for this - it’s almost certainly not going to be JIT compiled, and you also have no idea what’s going on in the underlying libraries (maybe ask the maintainer! 😉) that could cause visibility to happen. It has no bearing on your point, or the OP’s problem! (it’s also going to break with P2D / P3D if you have a slow system)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 13, 2018, 9:18am UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/16 "2018-08-13T09:18:31Z")

</div>

> [@neilcsmith](#):
>
> I have no desire to go over the same argument with you again…

I agree! But it’d be cool if you’d come up w/ some code example which demos your point. 🕶

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [August 13, 2018, 9:51am UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/17 "2018-08-13T09:51:06Z")

</div>

> [@GoToLoop](#):
>
> I agree! But it’d be cool if you’d come up w/ some code example which demos your point.

This thread! Probably. Any code that goes counter to the JMM is _potentially_ buggy. You’re asking for a code example that consistently demonstrates something that is by definition not guaranteed to be consistent! You’ll find enough examples of problematic code if you google for it. Some things may fail almost immediately, some after enough time for the JIT to really kick in (minutes or hours), some almost randomly.

It’s quite simple, really - follow the spec! 😄

---

<div class="post-metadata">

**Author:** ![iamtomodea](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/iamtomodea/32/1054_2.png) [@iamtomodea](https://discourse.processing.org/u/iamtomodea)\
**Post date:** [August 13, 2018, 10:12am UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/18 "2018-08-13T10:12:57Z")

</div>

Hi.

Thanks, this is really useful information. I read the spec you linked above: [https://en.m.wikipedia.org/wiki/Java\_memory\_model](https://en.m.wikipedia.org/wiki/Java_memory_model)  
From what I can make of it the possible reordering of processes at compilation is computed based on happens-before order. I think I get this, but presumably there is no way to really work out what the order is between threads.  
In which case - is the advice always to mark a variable as volatile if it is used by multiple threads? Or at least if their completion requires that a thread may have a specific value?  
Thanks

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [August 13, 2018, 10:32am UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/19 "2018-08-13T10:32:27Z")

</div>

> [@iamtomodea](#):
>
> is the advice always to mark a variable as volatile if it is used by multiple threads?

Yes, or something that has the same guarantees, such as accessing inside a synchronized block (on both threads, on the same lock!) or getting more complicated, the various other locks and atomic things in `java.util.concurrent` (eg. there is an [AtomicBoolean](https://docs.oracle.com/javase/8/docs/api/index.html?java/util/concurrent/atomic/AtomicBoolean.html) that can atomically compare and set)

> [@iamtomodea](#):
>
> Or at least if their completion requires that a thread may have a specific value?

If you have a situation where two threads are reading / writing to the same variable and each thread _doesn’t_ care about the other, you probably shouldn’t be using a shared variable in the first place! 😄

---

<div class="post-metadata">

**Author:** ![iamtomodea](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/iamtomodea/32/1054_2.png) [@iamtomodea](https://discourse.processing.org/u/iamtomodea)\
**Post date:** [August 13, 2018, 10:41am UTC](https://discourse.processing.org/t/while-loop-bug-stuck-on-empty/2557/20 "2018-08-13T10:41:05Z")

</div>

> Yes, or something that has the same guarantees, such as accessing inside a synchronized block (on both threads, on the same lock!) or getting more complicated, the various other locks and atomic things in `java.util.concurrent` (eg. there is an [AtomicBoolean](https://docs.oracle.com/javase/8/docs/api/index.html?java/util/concurrent/atomic/AtomicBoolean.html) that can atomically compare and set)

Grand, I think I can manage it with volatile for now anyway.

> f you have a situation where two threads are reading / writing to the same variable and each thread _doesn’t_ care about the other, you probably shouldn’t be using a shared variable in the first place!

Yeah, I suppose I can’t think of anything where this is the case - I guess I was thinking about something where the variable was being read for some less important reason that didn’t require it to be fully synced with the timing of the other thread. But obviously it still does cares.  
Anyway, thanks for all the help. I hadn’t come across volatile variables before so it’s great to understand them (somewhat).
