> 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
--
R. Vince - 13 Jun 2007 15:07 GMT
Ahhh, yes yes. Thanks Gordon. I knew I was missing something dopey here.
Thanks for sparing me tons of frustration today!
> 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
[quoted text clipped - 5 lines]
> 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.

Signature
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
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;
}
Lew - 13 Jun 2007 17:49 GMT
> 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
[quoted text clipped - 5 lines]
> public Runnable studyinteractive;
> ...
Why not provide an /actual/ SSCCE?
> public void doResponseStudy() {
> studyinteractive = new Runnable(){
[quoted text clipped - 20 lines]
> return b;
> }
Have you considered reading the Javadocs?
<http://java.sun.com/javase/6/docs/api/java/lang/Object.html#notify()>
> 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?

Signature
Lew
Matt Humphrey - 13 Jun 2007 17:50 GMT
| 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
[quoted text clipped - 8 lines]
| at java.lang.Object.notify(Native Method)
| at
robot.server.core.responder.SmartResponder.getGoBack(SmartResponder.java:411)
| public class SmartResponder {
| public Runnable studyinteractive;
[quoted text clipped - 22 lines]
| 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 matth@ivizNOSPAM.com http://www.iviz.com/