Thread.wait()

R

R. Vince

If I have a Runnable with a do{}while loop within it, and I have an
ArrayList of objects which is being operated on in the Event Dispatch
thread, and within the loop within my Runnable, I Thread.wait() until that
ArrayList is down to 0 elements within it, to continue through the
loop.....as my code below (should, I believe) perform, can anyone tell me,
why, then, when my arrayList IS 0 size, the do{}while loop does not seem to
continue, i.e. I dont seem to come out of the wait() state? Must I do
something to break out of the wait() state aside from having
arrayList.size()==0 ? Thanks, RalphVince


public ArrayList arrayList =new ArrayList();
//arrayList is assigned some values
Runnable studyinteractive = new Runnable(){
public void run(){
do{
//...something
try{
synchronized(this) {
while(arrayList.size()>0) //arrayList is being
worked on in another thread
wait();
}
}catch(InterruptedException interruptedexception) { }
}while(somethingIsTrue);
};
};
javax.swing.SwingUtilities.invokeLater(studyinteractive);
 
G

Gordon Beaton

I dont seem to come out of the wait() state? Must I do something to
break out of the wait() state aside from having arrayList.size()==0?

You are stuck in wait() until another thread invokes notify() on the
same object (in your example, the object referred to as "this"). It
should do that after modifying the ArrayList whose size you are
checking.

/gordon

--
 
C

Christopher Benson-Manica

R. Vince said:
If I have a Runnable with a do{}while loop within it, and I have an
ArrayList of objects which is being operated on in the Event Dispatch
thread, and within the loop within my Runnable, I Thread.wait() until that
ArrayList is down to 0 elements within it, to continue through the
loop.....as my code below (should, I believe) perform, can anyone tell me,
why, then, when my arrayList IS 0 size, the do{}while loop does not seem to
continue, i.e. I dont seem to come out of the wait() state? Must I do
something to break out of the wait() state aside from having
arrayList.size()==0 ? Thanks, RalphVince

In addition to the previous response, if you're filling up the list
and emptying it once, you might look at a CountDownLatch as a more
convenient way to wait for the list to become empty.
 
R

R. Vince

Ahhh, yes yes. Thanks Gordon. I knew I was missing something dopey here.
Thanks for sparing me tons of frustration today!
 
R

R. Vince

I'm struggling getting my head around this for some reason today. I'm not
referring to things correctly i n my code, with respect to the threads -- I
must be misunderstanding you Gordon, because I am getting and
IllegalMonitorStateException when I call notify on the runnable, from, what
I believe, is the class that owns the Runnable, yes? I've pared down the
code here, but you can see what is going on. What am I missing? The thread
calling the notify IS the Event Dispatch thread, isn;t it? If not, how do I
obtain it from where I am at below? Thanks again -- I'm just dizzy from this
or something! -Ralph

Exception in thread "AWT-EventQueue-0"
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notify(Native Method)
at
robot.server.core.responder.SmartResponder.getGoBack(SmartResponder.java:411)

public class SmartResponder {
public Runnable studyinteractive;
....
public void doResponseStudy() {
studyinteractive = new Runnable(){
public void run(){
study();
};
};
javax.swing.SwingUtilities.invokeLater(studyinteractive);
}
public void study(){
...
try{
synchronized(this) {
while(getGoBack())
wait();
}
}catch(InterruptedException interruptedexception) { }
}
public boolean getGoBack(){ //pared down here, b is actually a long
conditional, not merely what is shown
boolean b= arrayList.size()>0;
if(!b )
studyinteractive.notify(); //// <-----------line 411
return b;
}
 
L

Lew

R. Vince said:
I'm struggling getting my head around this for some reason today. I'm not
referring to things correctly i n my code, with respect to the threads -- I
must be misunderstanding you Gordon, because I am getting and
IllegalMonitorStateException when I call notify on the runnable, from, what
I believe, is the class that owns the Runnable, yes?

Not relevant. The question for notify() is "who owns the monitor?" It's got
nothing to do with Runnable.
Exception in thread "AWT-EventQueue-0"
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notify(Native Method)
at
robot.server.core.responder.SmartResponder.getGoBack(SmartResponder.java:411)

public class SmartResponder {
public Runnable studyinteractive;
...

Why not provide an /actual/ SSCCE?
public void doResponseStudy() {
studyinteractive = new Runnable(){
public void run(){
study();
};
};
javax.swing.SwingUtilities.invokeLater(studyinteractive);
}
public void study(){
...
try{
synchronized(this) {
while(getGoBack())
wait();
}
}catch(InterruptedException interruptedexception) { }
}
public boolean getGoBack(){ //pared down here, b is actually a long
conditional, not merely what is shown
boolean b= arrayList.size()>0;
if(!b )
studyinteractive.notify(); //// <-----------line 411
return b;
}

Have you considered reading the Javadocs?
Wakes up a single thread that is waiting on this object's monitor.

This object being the one on whom nofify() is called, i.e., "studyinteractive"
in your case. Since "studyinteractive" doesn't own the monitor, it isn't able
to do that.

Think of "notify()" as a kind of random release() call, "I'm releasing my
monitor now!" sent to an arbitrary Thread.

Your use of the "studyinteractive" variable (which, btw, should be spelled by
convention with initial capital letters to kick off the word parts, i.e.,
"studyInteractive") confuses me, and probably the JVM, too. Your Runnable
seems to want to be the one running getGoBack() in its own Thread, yet you
call notify() not through 'this' but through the instance variable.

"notify()" is meant to be called from 'this'.
This method should only be called by a thread that is the owner of this object's monitor.

Ain't the Javadocs wonderful?
 
M

Matt Humphrey

"R. Vince" <rvince99 a t hotmail d o t com> wrote in message
| I'm struggling getting my head around this for some reason today. I'm not
| referring to things correctly i n my code, with respect to the threads --
I
| must be misunderstanding you Gordon, because I am getting and
| IllegalMonitorStateException when I call notify on the runnable, from,
what
| I believe, is the class that owns the Runnable, yes? I've pared down the
| code here, but you can see what is going on. What am I missing? The thread
| calling the notify IS the Event Dispatch thread, isn;t it? If not, how do
I
| obtain it from where I am at below? Thanks again -- I'm just dizzy from
this
| or something! -Ralph
|
| Exception in thread "AWT-EventQueue-0"
| java.lang.IllegalMonitorStateException: current thread not owner
| at java.lang.Object.notify(Native Method)
| at
|
robot.server.core.responder.SmartResponder.getGoBack(SmartResponder.java:411)
|
| public class SmartResponder {
| public Runnable studyinteractive;
| ...
| public void doResponseStudy() {
| studyinteractive = new Runnable(){
| public void run(){
| study();
| };
| };
| javax.swing.SwingUtilities.invokeLater(studyinteractive);
| }
| public void study(){
| ...
| try{
| synchronized(this) {
| while(getGoBack())
| wait();
| }
| }catch(InterruptedException interruptedexception) { }
| }
| public boolean getGoBack(){ //pared down here, b is actually a long
| conditional, not merely what is shown
| boolean b= arrayList.size()>0;
| if(!b )
| studyinteractive.notify(); //// <-----------line 411
| return b;

Just like wait(), you must own the monitor before you can invoke notify().
You need to wrap this fragment with synchronized. You are also using 2
different monitors--the wait is on "this" SmartResponder while the notify is
on studyInteractive Runnable. You must use the same object for both.

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top