Sleep a thread until an event is attended in another thread from a different class

Posted by Afro Genius on Stack Overflow See other posts from Stack Overflow or by Afro Genius
Published on 2010-05-08T09:58:33Z Indexed on 2010/05/08 10:18 UTC
Read the original article Hit count: 189

I have an application that fires 2 threads, the 1st launches another class to do some processing which in turn launches a 3rd class to do yet more processing. The 2nd thread in the main class should wait until some event in the 3rd class completes before it performs its job. How can this be achieved?

I had tried implementing a wait/notify to share a lock object between the two threads but technically this will not work as I found the hard way. Can I share a lock between classes? Note, an instance of the 3rd class is declared in the 1st class and passed as parameter to the 2nd class. Also I tried creating boolean value in 3rd class that tells when event is complete then poll 2nd thread till this value is true. This worked but is not very desirable. Also is actionListner a better approach to this problem?

© Stack Overflow or respective owner

Related posts about java

Related posts about threads