• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

how do i interrupt a running thread

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
How do i stop a thread instead of waiting for it reach the end of run() method. Here are some cases:
A) A thread is waiting on some object by calling someobject.wait();
B) A thread is being Blocked on IO routin, say network stream read and write.
C) A thread is busy within a while(true){} loop doing its fine works.
D) ... and some other situations i still dont know.
Would you please help me with these problems?
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.
C) A thread is busy within a while(true){} loop doing its fine works.
Check a parameter in the while condition instead of true that you can set from the outside. while(a==0) {} with "a" being a public variable in your thread class. Setting "a" to 1 will terminate your thread after the current loop finished.
A) A thread is waiting on some object by calling someobject.wait();
Wrap the wait() inside a loop as described in C, and specify a timeout for wait, e.g. someobject.wait(5000); to wait 5 seconds. Then you can test an abort condition in the while() and you can even set a timeout inside your thread class when the loop has run a certain number of times without the wait() being interrupted by notify(). You have to catch InterruptedException that tells you whether wait() got a timeout (5000 ms) or an interruption.
B) A thread is being Blocked on IO routin, say network stream read and write.
I do not know. I think, blocked means blocked. You cannot do much besides waiting outside the thread for the execution of the IO request and proceed if a timeout occurs. Maybe the thread gets deleted when the reference is deleted but I don't think so.
Christian Wolf
[edit 2]
[ January 19, 2004: Message edited by: Christian Wolf ]
 
author
Posts: 1436
6
Python TypeScript Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Christian. You need to use some condition inside the run() method to determine when to abort. Java used to have a "Thread.stop()" method but it is not safe and not supported in J2ME.
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


B) A thread is being Blocked on IO routin, say network stream read and write.


For this situation, I recommend that in a second thread you close the IO stream that the blocked thread is using, for example: stream.close(). You should also catch in the IO thread the IOException that will be thrown when the close() gets done in the second thread. It is also good practice to catch any IOException that can be thrown in the second thread, the one calling the stream.close() method, in case it is thrown for some reason ... which often occur with IO.
 
Christian Wolf
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For this situation, I recommend that in a second thread you close the IO stream that the blocked thread is using, for example: stream.close(). You should also catch in the IO thread the IOException that will be thrown when the close() gets done in the second thread.
Interesting idea. But if the IO is hanging at HttpConnection.getResponseCode() and the remote server is not reachable, then I cannot close any stream as the outputstream for sending data has already been closed before and the input stream is opened after getting the responsecode only. The HttpConnection itself times out after ~20 seconds (on my emulator) and throws an IOException (Network read error).
It is also good practice to catch any IOException that can be thrown in the second thread, the one calling the stream.close() method, in case it is thrown for some reason ... which often occur with IO.
I am also not quite sure how it works that I open the connection in one thread and catch thrown exceptions in another one. Can you please describe this a bit more?
Christian Wolf
 
Christian Wolf
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For this situation, I recommend that in a second thread you close the IO stream that the blocked thread is using


Boah, that was easy. Inside my Http thread class that opens the HttpConnection, I specified a private sub thread (TimeOutThread) that I start before I open the HttpConnection. The first action of TimeOutThread is sleep(10000). After these 10 seconds, I check for a variable that is changed after responseCode() returns. So I know whether HttpConnection still tries to connect to the remote server. If this is the case, I just call HttpConnection.close(). In my main thread, I get an IO exception ("user abort") and the thread finishes. Great! Thanks.
Before I made this, I had a timeout specified in the UI thread. Very stupid, of course.
What is a reasonable setting for the timeout? 10 seconds? 15 seconds? My application will run on a 3G network.
Christian Wolf
 
This will take every ounce of my mental strength! All for a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic