Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Stopping a Thread (where I don't have control of run) Help needed  RSS feed

 
hallian
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a main program in which I create five more threads. I am creating and starting threads like this:..
##############################################
//loop
Runnable target = (Runnable) runnableObjects.get(count);
Thread newThread = new Thread(target);
newThread.start();
//end loop
###############################################
The run method is common and implements the Runnable Interface run method. When these threads are created I show a Dialogbox with ProgressBars for each of the threads showing the status. Means if I have five threads I show five ProgressBars one for each thread that shows the status of the threads completion. I also have a Button that when clicked stops all the threads and closes the Dialogbox. But my problem is that when I click a Button the Dialogbox closes but the Threads still keep on running. How can I kill or stop all the threads when a Button is clicked. I really apprecitae for any help. I tried to use
################
Thread.stop()
################
it does work but it's deprecated.
Here is what I used to stop all the threads if I use the stop() method.
##########################################
//loop
((Thread) e.nextElement()).stop();
//end loop
##########################################
But the stop method is deprecated. Is there any other way to stop and kill the threads.I read the Docs. that say to use a variable and that when changes exits the run method. I did tried that but doesn't help me in my case. Here is the code snippet for the run method:..
##################################
boolean stopped = false;
public void run()
{
if (stopped)
{
return;
}
someOtherClass.aMethod();
}
##################################
Now in my Button click event I set the stopped variable to true but how it helps me in my run method. As when the thread starts it checks the variable it's false it continues and executes aMethod() now let's say current execution is in aMethod() and we set the stopped variable to true now how can we stop all the threads at this time and return from the run method and stop all the threads as we are in the aMethod(). Means if I have five threads then each uses this run method and all the threads are started at the same time. When I click the Button it stops all the Threads even if I am in aMethod(). How can I achieve this. Any help is really appreciated.
Thanks
 
sim sim
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prog,
It would be more helpful if you paste your code.
Lets say I have a class which implements Runnable interface.
class MyThread implements Runnable {
private boolean running = true;
....
...... //ur code goes here....
public void run() {
while(running) { //keeps in an infinite loop
//do what ever you want to do.
}
}
public void stopRunning() {
//sets the running to false and run() exits
running = false;
}
}
public class ThreadHandler {
//creat an array to hold 5 threads.
Thread mythread = new Thread[5];
for(int ni = 0;ni<5;ni++ ) {

//create an object of class implementing Runnable
MyThread m = new MyThread();

//add it to the array
mythread[ni] = m;

//create a new thread object with the runnable class as argument and start.
new Thread(m).start();
}

public vodi action performed(ActionEvent e) {
for(int ni = 0;ni<5;ni++) {

//get the thread form the array, this thread is runnig now.
MyThread m = mythread[ni];
//call the stop method which will set the running to false.
m.stopRunning();
}
}
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!