Article View: borland.public.jbuilder.java.language
Article #8603Killing a blocked thread...
From: Kashif Dastgir
Date: Sun, 11 Mar 2001 17:46
Date: Sun, 11 Mar 2001 17:46
52 lines
2598 bytes
2598 bytes
Hello
I'm having a problem at hand related to killing a thread that is currently
waiting for return from a blocking call of some other class... The code is
something like...
class MyThread extends Thread
{
private Input input = null;
private boolean keepRunning = true;
MyThread (Input input) throws Exception
{
if (input == null) throw new Exception("Null arguments");
this.input = input;
}
public run ()
{
while (keepRunning)
{
String str = input.getInput(); // a blocking method call
// Some other code
}
}
public kill ()
{
this.keepRunning = false;
}
}
In the code, the class Input is an external class and it's code is not
available. We are making a call to the getInput() method which is blocking,
and it wont return unless there is some input available inside the input
object. Now assuming that the input object passed to the constructor is a
shared object in the system and getInput() is a synchronized method. This
implies that apart from my thread, there can be many other threards using
the same object.
Now I just want to kill my thread (cuz i'm no longer interesting in getting
any input from the input object). However, the input object being a shared
object will stay and the other threads in the system will still keep waiting
for an input from that object. I just want to kill MY thread. How can i do
that. In the above example, i can call the kill() method of my thread which
will set the loop invariant to false, but it wont help unless the function
call from the blocking method is returned, and the loop invariant will only
be checked afterwards. Is there a way to get the control back from a
blocking call.
Regards
Kashif
Technical Details
Message-ID:
<98gdng$3s62@bornews.inprise.com>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!bornews.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,ce7e5a0605d83b15,start X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-03-11 09:54:19 PST Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!bornews.borland.com!not-for-mail From: "Kashif Dastgir" <kdastgir@yahoo.com> Newsgroups: borland.public.jbuilder.java.language Subject: Killing a blocked thread... Date: Sun, 11 Mar 2001 22:46:10 +0500 Organization: Another Netscape Collabra Server User Lines: 55 Message-ID: <98gdng$3s62@bornews.inprise.com> NNTP-Posting-Host: 203.135.14.130 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200