Last active
December 21, 2015 04:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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