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 / First Aid / May 2004

Tip: Looking for answers? Try searching our database.

Threads, wait(), notify() problem...

Thread view: 
Mark \(UK\) - 07 May 2004 13:40 GMT
Hi all,

I have written a program which successfully uses threads to run an iteration
whilst at the same time responding to input from a simple GUI. However i
need to rewrite it without using suspend() and resume() to avoid deadlocks.
The 2 threads start fine, and i can use a button event in the GUI thread to
pause the iteration thread using wait(). However whatever i try i cannot
make the button rewake the iteration thread using notify().

I'll paste the critical parts of the code below, any help very much
appeciated here!

Thanks

Mark

/////////////////////////////////  DECLARING THREAD & BOOLEAN

 private Thread iteration_thread = new Thread(this);

 private volatile boolean threadSuspended;

////////////////////////////////////////////////////////////////  TOGGLE
BUTTON

       toggleButton = new Button("Toggle");
       toggleButton.addActionListener(new ActionListener()
        {
          public synchronized void actionPerformed(ActionEvent e)
           {
             if(!iteration_thread.isAlive()) iteration_thread.start();
             else
              {
                threadSuspended = !threadSuspended;
                if (!threadSuspended)
                 {
                   notify();
                 }
              }
           }
         });

/////////////////////////////////////////////////  THE THREAD'S RUN METHOD
   public void run()
    {
       while (true)
        {
           try
            {
              Thread.currentThread().sleep(500);

              synchronized(this)
               {
                 while(threadSuspended)
                  {
                    wait();
                  }
               }
              System.out.println("here");     // what to do when not paused
             }
           catch(InterruptedException e)
            {

            }

       }
   }
Mark \(UK\) - 07 May 2004 15:27 GMT
Mark (UK) wrote:
> Hi all,
>
[quoted text clipped - 12 lines]
>
> Mark

[code removed - see 1st post]

ok i've investigated some more, and i think the problem might be do do with
thread owenership? If i change notify() to iteration_thread.notify() I get
the error: "java.lang.IllegalMonitorStateException: current thread not
owner".

Any ideas?

Cheers
Mark \(UK\) - 07 May 2004 16:49 GMT
Mark (UK) wrote:
> Mark (UK) wrote:
>> Hi all,
[quoted text clipped - 24 lines]
>
> Cheers

It's ok I solved it, had to move the notify() out from the event handler
into a new method.


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.