The Artima Developer Community
Sponsored Link

Java Answers Forum
Waiting for Thread/Runnable Close/Stop confirmation before enabling Start

2 replies on 1 page. Most recent reply: Apr 14, 2005 4:46 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Shaitan

Posts: 11
Nickname: shaitan00
Registered: Feb, 2005

Waiting for Thread/Runnable Close/Stop confirmation before enabling Start Posted: Apr 13, 2005 10:52 PM
Reply to this message Reply
Advertisement
Coding Style: NetBeans IDE 4.0 Beta2 (Java)

My GUI has a button (start/stop) that can either START (spawn Thread/Runnable) or STOP (stop Thread/Runnable).

The way I implemented stop (as per all the feedback) was using a boolean. So my thread/runnable has a WHILE(run==true) and the .STOP method changes the boolean run==false.

This seems to work great, I put a Timeout on my thread (10seconds) so the loop is forced to run every 10seconds (incase it gets stuck and I want to stop it) - FYI: the thread/runnable has a DataSocket.recieve (that is what the Timeout is for, incase the thread/runnable is waiting for a response and I still want to stop it so I force a SocketException: Recieve Time out (BTW: is it bad to have these? I put the timeout to 10seconds so I get a lot of exception [not that many recieves when testing], is this Bad practice?)

Anyways, back to the real question.
My current problem is this scenario:

- User presses start
- Thread/Runnable launches
- everyone chats, all is good, all is happy
- User presses Stop
- Immediatly after user presses Start again
ERROR....(thread exception)

The Error is because the Thread I stopped had not actually stopped yet (needs 10 seconds) but the user tries to Start it again anyways.. So I need a way to DISABLE the START button until I am sure the thread has stopped...

I tried something like this:
// Stop Server
sMain.stop(); // This is my Thread/Runnable
startStop.setEnabled(false); // Disable button so user cannnot click
while (sMain != null){} // It seems this doesn't work....
startStop.setEnabled(true); // Enable button to allow start again
startStop.setLabel("START");

All this did was use 100% CPU until I end-tasked. How can I accomplish this task?

[Pseudo-Code]
--THREAD/RUNNABLE serverMain (sMain) --
while (run==true)
{
// Do all the work
}

public void stop()
{
run = false; // so the next time the while loop runs it will exit then the thread will finish and should become NULL no?
}
[Pseudo-Code]


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Waiting for Thread/Runnable Close/Stop confirmation before enabling Sta Posted: Apr 14, 2005 2:02 AM
Reply to this message Reply
Your idea is good. while (run) is exactly what you need to do.

The solution is: use 2 boolean variables.
This: while (sMain != null){} won't work, because you never set sMain = null;
class MyClass extends Runnable () {
   boolean runThread = true;
   boolean threadStopped = true;
   public void run() {
       while (runThread) {
           threadStopped = false;
           //... do something important
       }
       threadStopped = true;
   }
   public void stop() {
       runThread = false;
   }
   public getThreadInactive() {
      return threadStopped;
   }
}



If the run method often does nothing, put it in a sleep state and use notifyif if it should start working again.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Waiting for Thread/Runnable Close/Stop confirmation before enabling Sta Posted: Apr 14, 2005 4:46 AM
Reply to this message Reply
One problem remains: You probably will wait for the Thread to wait like that:
while(myClassInstance.getThreadInactive());

That causes a massive processor usage. The solution is to put everything the Thread is doing in a synchronized method and also make getThreadInactive a synchronized method.
This way your main program will only continue when the doSomething method is finished.


class MyClass extends Runnable () {
   boolean runThread = true;
   boolean threadStopped = true;
   public void run() {
       while (runThread) {
           threadStopped = false;
           doSomething(); //while this method is active, any other synchronized method can be called, but won't be executed until this one ends.
       }
       threadStopped = true;
   }
   private synchronized void doSomething() {
       //...do Something
   }
   public void stop() {
       runThread = false;
   }
   public synchronized boolean getThreadInactive() //waits for doSomething() to stop
      return threadStopped;
   }
   public boolean getThreadInactiveRealTime() {
      return threadStopped;
   }
 
}



One more thing: If you post code, please use the java tags as shown on the right side of the input window.
That makes the code easier to read.

Flat View: This topic has 2 replies on 1 page
Topic: Holding on to Threads/Runnables (ArrayList) to be able to .STOP them Previous Topic   Next Topic Topic: How to get GUI panels out of another class?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use