Skip to content

Instantly share code, notes, and snippets.

@jorgeortiz85
Created August 12, 2011 15:41
Show Gist options
  • Save jorgeortiz85/1142319 to your computer and use it in GitHub Desktop.
Save jorgeortiz85/1142319 to your computer and use it in GitHub Desktop.
The bug from hell
// 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