This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Mock question. Can anyone provide the answer How could you coordinate two threads that execute simultaneously inside synchronized methods of the same object? Choice 1 use Thread.wait() and Thread.notify() Choice 2 dynamically control the priorities of the threads within the method Choice 3 use Thread.sleep() Choice 4 place the threads in the same ThreadGroup and let the ThreadGroup acquire the object's monitor Choice 5 use Thread.yield() to switch between threads at set points
Answer is 1. When you have two threads and they are using synchronized methods, you are dealing with locks. 1. This is how you lock ( wait() ) and release ( notify() and notifyAll() ) locks 2. Some systems will allow this to work, but time-slicing systems won't. They allow threads of lower priority to kick in every now and then. So this won't work on Windows for example. 3. sleep() doesn't release locks. 4. Don't know much about ThreadGroups, but I am fairly sure they don't deal with locks. 5. yield() doesn't release locks. Bill