From: Ian Semmel on
public void run ()
{
this.taskQ = new LinkedBlockingQueue<Object> ();

try
{
taskQ.wait ();
}
catch ( InterruptedException ie )
{
System.out.println ( ie.getMessage () );
System.exit ( 255 );
}
}

Exception is "java.lang.IllegalMonitorStateException: current thread not owner" on the wait()

Seeing as how I just created the Q, what thread is the owner ?

PS I don't know much about java.
From: Mark Space on
Ian Semmel wrote:
> public void run ()
> {
> this.taskQ = new LinkedBlockingQueue<Object> ();
>
> try
> {
> taskQ.wait ();
> }
> catch ( InterruptedException ie )
> {
> System.out.println ( ie.getMessage () );
> System.exit ( 255 );
> }
> }
>
> Exception is "java.lang.IllegalMonitorStateException: current thread not
> owner" on the wait()
>
> Seeing as how I just created the Q, what thread is the owner ?
>
> PS I don't know much about java.

Gotta hold the lock before you can wait on it.

try {
sychronized( taskQ ) {
taskQ.wait();
}
} // ...

I think. It does not matter that the variable is local, you still must
synchronize.

There might be better ways of synchronizing on a BlockingQueue, I
haven't used that particular class.
From: Ian Semmel on


Mark Space wrote:
> Ian Semmel wrote:
>> public void run ()
>> {
>> this.taskQ = new LinkedBlockingQueue<Object> ();
>>
>> try
>> {
>> taskQ.wait ();
>> }
>> catch ( InterruptedException ie )
>> {
>> System.out.println ( ie.getMessage () );
>> System.exit ( 255 );
>> }
>> }
>>
>> Exception is "java.lang.IllegalMonitorStateException: current thread
>> not owner" on the wait()
>>
>> Seeing as how I just created the Q, what thread is the owner ?
>>
>> PS I don't know much about java.
>
> Gotta hold the lock before you can wait on it.
>
> try {
> sychronized( taskQ ) {
> taskQ.wait();
> }
> } // ...
>
> I think. It does not matter that the variable is local, you still must
> synchronize.
>
> There might be better ways of synchronizing on a BlockingQueue, I
> haven't used that particular class.

Success! Thank you.

Probably is better ways, I am still learning. Anyway, this works.
From: Lew on
Ian Semmel wrote:
>
>
> Mark Space wrote:
>> Ian Semmel wrote:
>>> public void run ()
>>> {
>>> this.taskQ = new LinkedBlockingQueue<Object> ();
>>>
>>> try
>>> {
>>> taskQ.wait ();
>>> }
>>> catch ( InterruptedException ie )
>>> {
>>> System.out.println ( ie.getMessage () );
>>> System.exit ( 255 );
>>> }
>>> }
>>>
>>> Exception is "java.lang.IllegalMonitorStateException: current thread
>>> not owner" on the wait()
>>>
>>> Seeing as how I just created the Q, what thread is the owner ?
>>>
>>> PS I don't know much about java.
>>
>> Gotta hold the lock before you can wait on it.
>>
>> try {
>> sychronized( taskQ ) {
>> taskQ.wait();
>> }
>> } // ...
>>
>> I think. It does not matter that the variable is local, you still
>> must synchronize.
>>
>> There might be better ways of synchronizing on a BlockingQueue, I
>> haven't used that particular class.
>
> Success! Thank you.

The error message contained the answer.

"current thread not owner [of the monitor]"

To own the monitor, a thread must synchronize on the object. Ergo, the thread
had not synchronized on the object that was the wait() target, 'taskQ'. To
fix not being synchronized on the 'taskQ' object, the thread must synchronize
on it.

The curious thing is to guess who will be notified when the monitor is
released. For wait()/notify() to work, there must be at least two threads
synchronizing on the same monitor. If you don't have that, then you don't
have the "Success!" that you think you do.

--
Lew
From: Patricia Shanahan on
Ian Semmel wrote:
> public void run ()
> {
> this.taskQ = new LinkedBlockingQueue<Object> ();
>
> try
> {
> taskQ.wait ();
> }
> catch ( InterruptedException ie )
> {
> System.out.println ( ie.getMessage () );
> System.exit ( 255 );
> }
> }
>
> Exception is "java.lang.IllegalMonitorStateException: current thread not
> owner" on the wait()
>
> Seeing as how I just created the Q, what thread is the owner ?
>
> PS I don't know much about java.

Given the PS, I'm going to question the assumption that you should be
calling taskQ.wait at all. For example, if you just want to suspend
until you can remove an item from taskQ, call taskQ.take().

The wait() method is part of the general, low level, suspension and
activation system that underlies structures such as a BlockingQueue.
Every object has a wait() method.

Perhaps you should describe what you are trying to achieve?

If you really do need to do taskQ.wait(), I agree with the previous
responses indicating that you need to be in a block that is synchronized
on the BlockingQueue.

Patricia