Created
August 12, 2011 15:41
-
-
Save jorgeortiz85/1142319 to your computer and use it in GitHub Desktop.
The bug from hell
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
// This code is unsafe to use in a multithreaded environment. | |
object A { | |
def foo = "foo" | |
B.bar | |
} | |
object B { | |
def bar = "bar" | |
A.foo | |
} | |
// SPOILER: | |
// | |
// Consider Thread-1 and Thread-2 initializing A and B, respectively, at the | |
// same time. Thread-1 running A's constructor needs to access B, and Thread-2 | |
// running B's constructor needs to access A. Since neither object has finished | |
// initializing, both threads will wait for the other to finish before they can | |
// continue, causing a deadlock. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment