I was asked this in an interview.
There are four threads t1,t2,t3 and t4. t1 is executing a synchronized block and the other threads are waiting for t1 to complete. What operation would you do, so that t3 executes after t1.
I answered that join method should do the trick, but it looks like it isn’t the right answer.The reason he gave was, the join method and the setPriority method would not work on threads that are wait state.
Can we achieve this? If yes, how?
I think i would use some latches. One countdownlatch between t1 and t2, another one between t2 and t3, the last one between t3 and t4. T1 Ends with countDown, and t2 starts the to be synchronized part with await.
That way all threads can do preprocessing in parallel and restore order for the sequential part.
I can’t say it’s elegant though.