• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can one thread halt another thread?

 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pardners,
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?
Thanks,
James
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!...
Cheers,
James
[ January 22, 2004: Message edited by: James H. ]
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
Basu.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basu,
Interuppting a thread performing i/o operation will not work since it is being ignored.
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic