Skip to content
Advertisement

Java Thread is blocked in join()

I have the following simple code in which I put and take from a Queue represented as an ArrayList.

JavaScript

I don’t understand why my thread is blocked in join() method. From main I call as follow:

JavaScript

Advertisement

Answer

Without running it…

  • The close() method holds lock at the time it calls thread.join() and waits on thread (forever)
  • thread is waiting to reacquire lock so cannot run

Both are now waiting on each other, this is a deadlock. Try moving the Thread.join() after the synchronized block:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement