Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Thread.wait()

Thread view: 
R. Vince - 13 Jun 2007 14:44 GMT
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);
Gordon Beaton - 13 Jun 2007 14:53 GMT
> 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!
Christopher Benson-Manica - 13 Jun 2007 15:06 GMT
> 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.

R. Vince - 13 Jun 2007 16:21 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? 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/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.