File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Can one thread halt another thread? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Can one thread halt another thread?" Watch "Can one thread halt another thread?" New topic

Can one thread halt another thread?

James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
I have a midlet which creates a thread, and if the user clicks 'Cancel' then I want the new thread to quit processing.
The new thread is called otaThread (it does some httpconnection stuff).
In my commandAction method I have:
public void commandAction(....) {
if (c == commandCancel) {
otaThread = null;
but the otaThread just keeps on going! Is there any way I can stop it. I have considered creating a flag (e.g., isCancelButtonPressed) which the otaThread could poll periodically, but is there a better way that anyone could suggest?
David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
That should have worked. Here is something else you can try in the run() method -
do {
...whatever code you need done }
while ( cancelNotHit );
Then, in the actionPerformed method of the cancel button, have the cancel button flip the true/false status of cancelNotHit. Just an idea!

David Crossett
-nothing important to say, but learnin' plenty-
James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
Thanks David,
The do-while will only check the flag at the end of the code within the loop, so I'd have to wrap every few lines of my OtaThread code in a do-while loop in order for it to get out, which is a bit impractical (and probably won't work knowing my luck...).
Back to the drawing board!...
[ January 22, 2004: Message edited by: James H. ]
V Chauhan
Ranch Hand

Joined: Nov 15, 2002
Posts: 70
You can interrupt the running thread when you want by invoking the method void interrupt().
In the thread which want to stop, you can catch the Interrupted Exception and do the cleanup which you want to do.
Let me know if it helps..
Murad Iqbal
Ranch Hand

Joined: Dec 09, 2003
Posts: 90
Yes, setting a flag should be enough to halt the process. I did something similar when I was implementing a Disaster Recovery System. I had to copy files from remote PCs on a network to a shared network drive. Now a file may be as big as 200 MB and as small as 2 KB. During the transfer process, if cancel button is pressed and the while loop checks a boolean to ensure the cancel was not pressed only before copying a new file, then it will take quite long for the program to stop the file copying thread if a file is of 200 MB (e.g. zip file). Therefore, what I did was that i checked that boolean while the inputstream was reading from the remote location and writing at the destination. Something like this:

byte[] buf = new byte[100];
while(i < NumberOfFilesToCopy && cancelPressed == false)
while((!file[i].EndOfFile && cancelPressed == false))
//reads for instance 50k data and writes to the remote file
buf = readFile(file[i]);
writeFile(file[i], buf);

hope this helps
Joe Nguyen
Ranch Hand

Joined: Apr 20, 2001
Posts: 161
Interuppting a thread performing i/o operation will not work since it is being ignored.
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
If you choose to use a boolean flag to indicate when it's time to stop, be sure to declare the flag volatile in case you should find your code running in a VM that keeps thread local copies of fields.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Joey Tran - I transferred several of your comments from this thread to this thread because they were about your own question, not the original question, and I thought they were confusing the issue here.
[ February 03, 2004: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Can one thread halt another thread?
It's not a secret anymore!