Skip to content

Instantly share code, notes, and snippets.

@jklingsporn
Last active May 8, 2017 15:16
Show Gist options
  • Save jklingsporn/62d7eb2e14cefa7a60f3d1ba2d6989e6 to your computer and use it in GitHub Desktop.
Save jklingsporn/62d7eb2e14cefa7a60f3d1ba2d6989e6 to your computer and use it in GitHub Desktop.
Locking gone wrong 1
public class LockTest {
private static final Logger logger = LoggerFactory.getLogger(LockTest.class);
public static void main(String[] args) throws InterruptedException {
ForkJoinPool.commonPool().execute(() -> { mayLock(127);});
ForkJoinPool.commonPool().execute(() -> { mayLock(127);});
ForkJoinPool.commonPool().shutdown();
ForkJoinPool.commonPool().awaitTermination(1, TimeUnit.MINUTES);
}
private static void mayLock(Integer arg) {
logger.info("entering method {} ",arg);
synchronized (arg){
logger.info("entering lock "+Thread.currentThread().getName());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment