Skip to content

Instantly share code, notes, and snippets.

@eribeiro
Last active December 21, 2015 04:29
Show Gist options
  • Save eribeiro/6250014 to your computer and use it in GitHub Desktop.
Save eribeiro/6250014 to your computer and use it in GitHub Desktop.
Can someone please explain why on earth is this guy synchronizing on Thread.sleep ? AFAIK, if you have, say, 4 threads, then one is sleeping while the other 3 are blocked, *waiting* to sleep (!!!) (detail: finished is not volatile). Your sincerely, Ed.
public class WTF extends Thread {
private boolean finished = false;
...
public void run() {
....
while (!finished) {
... [do work]
Thread.yield();
if (!finished) {
synchronized (this) { // WHY ?????
try {
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e) {
LOG.error(e);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment