-
How can a thread kill another blocked thread?
Hello,
As the title mentioned, lets say i a main class in which i created a new thread but for some reason, this thread is blocked so i want to find a way to kill this thread from the main class (the interrupt function is not working), is there a workarround to do this?
Thank you in advance
Flovet
-
Generally, there are two ways to kill a thread:
1. Set value of some shared var, so that the thread reads it and terminates voluntarily.
2. Invoke Thread.stop() which is deperecated and unsafe.
In your example, Thread.interrupt() does not help, so the thread is not locked, it is executing some code. So, you should add checks like (1) to that code.
Hope this helps
Denis.
--------------------
http://www.excelsior-usa.com/jet.html
JVM with AOT compilation
-
Hello,
The thread is in a runnable state, and it is executing some piece of code which never returns , like for exmaple:
SimpleThread task = new SimpleThread()
task.start();
on the other hand the SimpleThread's run method contain the following code:
.......
RemoteLogin rlogin = new RemoteLogin(); // call to a remote class
/*the initialization above does not return due to some network problems so the thread keeps on waiting for infinit time, i want to kill this thread, is there a good way to do this?*/
Thank in advance
-
i dont know about RemoteControl class,
generally
if a library doesn't offer sometihng better,
this logic may help.
Code:
public class SyncTest {
static boolean connected = false;
static int goodluck = 0;
static Random rand = new Random(System.currentTimeMillis());
public static void main(String[] args) {
Thread t = new Thread() {
public void run() {
// if random number is 1 we will be connected
while (goodluck != 1) {
System.out.println(".");
goodluck = rand.nextInt(1000);
};
connected = true;
}
};
t.start();
//wait either some seconds passed or thread finished
t.join(1000);
if (!connected) {
t.stop();//deprecated method stop()...
System.out.println("couldnt connect and terminated");
System.exit(1);
}
else {
System.out.println("connected");
};
}
}
-
If you know what resource the thread is hanging on, try closing it.
-
Hello,
I am working in a web environment, the code that i am executing is run within a jsp so the System.exit could stop the whole application and not just this particular thread
i tried the stop() method but still does not work, it is not killing the thread :S
any suggestions?
Thks for the code mr1yh1
-
I think the successful killing of a blocked thread is related to how its being blocked. If waiting for I/O close the resource.
What is your code blocking on?
Similar Threads
-
By chill_out in forum C++
Replies: 2
Last Post: 05-30-2005, 06:10 AM
-
Replies: 4
Last Post: 08-02-2002, 05:11 AM
-
By Lowell Williams in forum vb.announcements
Replies: 18
Last Post: 02-10-2001, 05:22 AM
-
Replies: 1
Last Post: 10-29-2000, 04:51 PM
-
Replies: 2
Last Post: 10-27-2000, 03:07 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks