Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

class Test { public synchronized void foo() throws InterruptedException { wait()

ID: 3660248 • Letter: C

Question

class Test { public synchronized void foo() throws InterruptedException { wait(); System.out.println("Goodbye"); } public synchronized void bar() throws InterruptedException { wait(); System.out.println("Hello"); } } Suppose two threads running concurrently share the same Test object. One thread invokes the bar method and the other thread invokes the foo method of the same object. What would be the result? a. The output will be "Goodbye" and the other thread waits forever until interrupted. b. The output will be "Hello" followed by "Goodbye" c. The two threads are deadlocked and wait forever until interrupted. d. The output will be "Hello" and the other thread waits forever until interrupted.

Explanation / Answer

The answer is:

c. The two threads are deadlocked and wait forever until interrupted.

Reason: the two methods are calling wait() and not notified later so they will be in waiting state for ever.