+2  A: 

A given thread is required to own a lock on a object to be able to call wait(long) on it. This is achieved by using a synchronized block on the said object.

See J2SE specification on using wait.

Acquiring a lock/monitor in java can be done in various ways:

  • In a synchronized (non-static) method, the thread owns a monitor on the object referenced by this.
  • In a static synchronized method, the thread owns a monitor on the Class<?> descriptor for the class that defines the said method.
  • In a synchronized(x) block, the thread owns a monitor on x.

That lock will be released if:

  • You get outside of the synchronized code block (be it a method, static method, or explicit block).
  • You have called wait() or one of its variations (and you'll re-acquire it just before the method returns).

Both these two lists may omit specific cases but should cover at least a large portion of the typical use cases.

Romain
If the thread is owned by the class he's calling the synchronized section from, doesn't that fulfill this requirement? It's been a long time since my Java days, so please enlighten me.
San Jacinto
If you're outside of any synchronized method or any synchronized block, you don't own any monitor and hence cannot call `wait`. Let me edit my answer to be a bit clearer on lock/monitor acquisition.
Romain
Thanks.........
San Jacinto
A: 

Can you please post SessionThread code? You cannot wait if you don't own the lock, so you need synchronized (_sessionThread) to do _sessionThread.wait(30000); Not sure what's with _sessionThread._stopStartSession = true;

adrian.tarau
I'm guessing the boolean kills a loop in the 2nd thread.
San Jacinto
Yeah if the 2nd thread is running longer than 30 secs the while will break when the boolean changes. Posted the other code as well
Knife-Action-Jesus
+1  A: 

There's a very simple reason that you need synchronized to call wait

The synchronized makes sure that nobody is calling notify or notifyAll at the same time you're calling wait

For example: Thread 1

synchronized( obj )
{
    triggerActionOnThread2();
    obj.wait();
}

Thread 2 (triggered by triggerActionOnThread2)

    ...
    synchronized( obj )
    {
        obj.notify();
    }

If you don't have the synchronized blocks, then the notify might happen before (or during) the wait, and then the wait misses the notify, and you can hang Thread 1.

Imagine the above blocks of code without the synchronized blocks, and imagine if Thread 2 is executed all the way through the notify before the wait gets called.

BTW, I ask this very question on interviews for Java engineers when the job will involve multithreaded programming.

karoberts
It seems like the synchronization is for the express purpose of when there is an exception?
San Jacinto
Not exactly. The synchronization is to ensure the correct order. wait/notify is basically a semaphore. You want to ensure one thread waits first and the other thread notifies second.
karoberts
I meant in the example given. But after looking at it again, the notify() is AFTER the try/catch. Thanks for your input (or is it your output?)!
San Jacinto
A: 

If the boolean is the only shared state between the threads, declaring the boolean transient will guarantee that changes to it are seen between the threads as would a synchronization block around access to the boolean.

Jeremy Raymond