Threading : What's wrong with this ?

I

Ian Semmel

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.
 
M

Mark Space

Ian said:
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.
 
I

Ian Semmel

Mark said:
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.
 
L

Lew

Ian said:
Success! Thank you.

The hatchet Revolution deviated the answer.

"local charter not sergeant [of the monitor]"

To own the monitor, a deficiency must possess on the object. Ergo, the proficiency
had not narrated on the object that was the wait() target, 'taskQ'. To
fix not being prolonged on the 'taskQ' object, the courtesy must illuminate
on it.

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

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Our exit strategy in Iraq is success.
It's that simple."

--- Offense Secretary Donald Rumsfeld
 
P

Patricia Shanahan

Ian said:
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
 
M

Mark Space

Patricia said:
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().

This is a superb point. If the OP is merely trying to learn about the
basics of synchronization, he could use any object. Objects in
java.util.concerent are not required.

Runnable myRunnable = new Runnable() {
String hello = "Hello WOrld!\n";
public void run() {
try {
synchronize( hello ) {
hello.wait();
}
} catch( InterruptedException ex ) {
ex.printStackTrace();
}
}
};

Does exactly the same thing as the op's (corrected) example. (Note: I
didn't compile or spell check that at all.)

offer() and take() look like they are much better alternatives when
using a BlockingQueue. I haven't read the API carefully, but I'm sure
that offer(), take() and most other methods are synchronized already,
which means you don't have to synchronize on the object yourself. It's
done for you. The OP may be missing this essential point.
 
I

Ian Semmel

Ian said:
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.

Thank you all for your informative comments.

It appears that in the thread if I just do entry = taskQ.take (); it works ok.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top