How to kill a dead thread
How to stop the following thread?
class MyThread {
public void run() {
neverReturn(); //no kidding, happend to me
private void neverReturn() {
while(true)
seems to me that "stop" is only way to go, right?
Fei Li
My question is: If I am not able to fix that bug (for
example: call a third paty library) can I stop the
thread without using stop?No.
But if this 3rd party library runs into a never-ending loop, why would you continue to use it (if you have another choice)? However, I know of what you speak - even Sun's own classes (URL/URLConnection) can lock up. But the bottom line is, "garbage in, garbage out". The root of the problem (infinite loop) still needs to get fixed or all you're doing is writing band-aid hacks to work around it.
Similar Messages
-
How to Kill Specific Execute Threads
Hi.
Through our application server, we noticed that there were some memory issues
where the memory usage would rapidly increase from ~ 15Mb to the max heap size.
Issuing a few thread dumps, we realized that there was a specific thread that
was churning. This was b/c someone sent a large request in that generated a lot
of data.
Anyways, since we couldn't bounce the weblogic server, we had to wait until the
request ended. Since we know the specific thread that was assigned to that job,
is there a way to kill the request associated with a specific execute thread and
reallocate back to the thread pool? We're running weblogic 6.1sp4. I think the
executequeueruntime bean just gives me back the current configuration of the all
the threads in the specific queue, which doesn't help me here.
Thanks.
--SelenaSelena,
how about get the ExecuteQueueRuntimeMBean and get all the execute
threads (getExecuteThreads() which returns an array of ExecuteThreads )
and since you know the thread id that is causing the bottleneck, you
could kill that thread.
thanks,
-satya
Selena wrote:
Hi.
Through our application server, we noticed that there were some memory issues
where the memory usage would rapidly increase from ~ 15Mb to the max heap size.
Issuing a few thread dumps, we realized that there was a specific thread that
was churning. This was b/c someone sent a large request in that generated a lot
of data.
Anyways, since we couldn't bounce the weblogic server, we had to wait until the
request ended. Since we know the specific thread that was assigned to that job,
is there a way to kill the request associated with a specific execute thread and
reallocate back to the thread pool? We're running weblogic 6.1sp4. I think the
executequeueruntime bean just gives me back the current configuration of the all
the threads in the specific queue, which doesn't help me here.
Thanks.
--Selena -
How to kill parent and child thread in sequnece
Hello freinds,
I have one class FirstClass which creates one thread Pserver and Pserver creates three threads then how to kill parent Pserver and child threads.........define a method that requests that the parent thread terminate itself, and within the code called when the thread is shutting itself down, have it do something similar to its child threads: call methods that request they shutdown themselves.
One common way to do this is to interrupt the thread. -
How to kill threads in obiee server
Can some one please tell me how to kill threads in OBIEE server
944632 wrote:
Thanks for the prompt reply. your answer is helpful. Here is my problem.
I am an OBIEE admin and getting lot of complaints fronm developers that BI Server services are not stopping. I was adviced by my senior that i have to kill thresads in the server.
I followed what you told but when i open the RPD, i am unble to open cache in either offline or online modeHi,
- Unable to open cache due to you already disabled cache (usage tracking - nqsconfig.ini file) cache diabled that why you can't able to open cache
just make sure
[CACHE]
ENABLE = YES;
C:\Oracle\Middleware\instances\instance1\config\OracleBIServerComponent\coreapplication_obis1 (obiee11g file path ref)
- unable to stop biservices? if that case you can kill it different ways, (stop bi services/ctrl +c on services/task manager/FMW (console/EM)/ services.msc etc) which way did you tried?
(which version did u tired)
Thanks
Deva -
Thread: How to tell when the thread is finished AND how to kill it?
Hi there,
I have a thread that runs in the background and does this:
class MyThread implements Runnable
public void run()
Stopper stopper = new Stopper();
//do something that takes some time
stopper.stop();
System.out.println("total time "+stopper.getTime());
}this works fine (I have a swing gui where I click a button and this thread runs and it's ok. PROBLEM IS, when I click the button again (to re-activate the thread) it seems that the stopper is not null but still counting the time (I was under the impression that ...= new Stopper() will create a new object type Stopper).
so -
1. How can I tell when thread is finished?
2. How do I kill this thread once it is finished?
the invocation:
public void actionPerformed(ActionEvent arg0)
Thread t = new Thread( new MyThread());
t.start();
}THANK YOU!You have not posted to code for Stopper, so it's impossible to reason about that
1. How can I tell when thread is finished?
Use Thread.Join() or Thread.isAlive()
2. How do I kill this thread once it is finished?
You can't and you don't. Just let the method the thread is running exit normally. Sometimes that means putting in a boolean variable to control a loop, and then exiting when the boolean is false.
Another thought - you could set the thread as a Daemon thread - when you don't have any non-daemon threads running, java will exit. -
Hello,
My question is simple how to kill a thread.
The background is a call to a method in a third party API, crawler4j, that locks the entire program.
The design of crawler4j is "You should stop it when you think it's enough (ctrl+c)" but as I use it only as a step in a larger program I want to stop it programmatically and not by "ctrl+c" as they have designed the API.
This is the locking callcontroller.start(MyCrawler.class, numberOfCrawlers);So I made a thread public class MyCrawlController extends Thread {
public void run(){
controller.start(MyCrawler.class, numberOfCrawlers);
}And in my main method
MyCrawlController mCC = new MyCrawlController();
mCC.start();
Thread.sleep(180000);
//Here I want to kill the mCC and all it's threadsOne problem is that my program never terminates. Probably due too the JVM not realizing that I don't want it to count mCC and it's spawned threads as alive. Any idea on how to solve this. For now I can only exit the program with System.exit(0); and I don't want that. So I need a way to stop the mCC's threads and get the JVM not to count them. Any idea on how to solve this. Even better if they can be taken by the GC.Farmor wrote:
Thanks for the answer.
I came to think about daemons and solved the problem with mCC.setDaemon(true);Really stupid that one has to do several programs and tie them together only to kill a thread.
As I only do this for other learning purposes and won't use this code or program after this week I will modify crawler4j, open source, and make myself a custom good method that will terminate itself and return to the main method properly.There are some fairly difficult problems with a number of aspects of concurrency if you forcibly stop threads, which is why Thread.stop() and related methods have long been deprecated.
It's a pity that Java can't respond to kill signals (like Ctrl-C) in a tidy way. I think they're all tied up with some of the JVMs internal processes.
In general people wind up using sockets instead of signals. -
I am facing the problem in killing the thread after some interval. If anybody knows how to kill the thread in middle plz give me reply
Thanks
SreedharThread.interrupt()
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thre
ad.html#interrupt() is not about killing or stopping
a thread. It is more about kicking a blocked thread.
There is no way to kill a thread. As been said
above, the thread should be designed to accept
instruction to stop gracefully.
The interrupt() was created by sun as a solution sun came up for the problems of thread stop(), it should be the cannonical way to stop a thread.
The problem with having your own mechanism of interrupting a thread is that you should have a method to interrupt a thread execution and a flag that should be checked from inside your thread or runnable code to see if it should stop and if so, do it cleanly. The problem with this aproach is that if the thread is sleeping or in some blocked state, calling your own interruption framework is inefective, as it could never get a chance to run the code.
To solve that, sun privides us with the method( interrupt() ) and the flag ( Thread.interrupted() ) with the advantage that on calling it, it guarantees you that it would take effect even if the thread is in a blocked state.
So, you are right about having to implement your own gracious interruption but the right way to do that is by checking interrupted(). And no Thread.interrupt() is not just about blocked threads, although it covers that ground.
May the code be with you. -
How can a thread kill another blocked thread?
Hello All,
As the title mentioned, lets say i have 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 advanceHello,
First of all, thanks for you interventions, i think i should clearify my case:
I have a website which has a "Search the site" feature where a user can search the site for specific keywords
The search engine is an external jar which we use by calling its API, but for some reason this search engine becomes blocked sometimes so my jsp which is calling this API gets also blocked and any subsequent search gets also blocked in the end the number of java threads becomes very high in my system!
i want to find a way so that if the piece of code calling the search API didnt get excuted during a specific period, i want to kill the thread calling the search API
lets say the search form is submitted to the following jsp
// Here we have a SearchThread extends Thread
/* i launched a new Thread to do the search independatly from the main process*/
SearchThread task = new SearchThread("search_input");
task.start();
/* In the searchThread class we have a run method containing the following line which calls the search API, for some reason , this line never returns and keeps the thread in Running state and so the run() method never returns and so the thread will neve be dead :( */
SearchLoginSession = new SearchLoginSession (configFile, true);
Notes:
1) the stop is deprecated i cant use it
2) the suspend method will suspend it and not kill it, the thread will remain in memory
3) the interrupt method didnt work
4)i am working in a multi-cpu environment
Finally , what i want is to find a way to remove this thread from memory from the main jsp listed above like
task.kill() or some workarround
Thank you for reading and hope you can help me -
How to "kill" AWT Event Queue thread without using System.exit()?
When I run my program and the first GUI window is displayed a new thread is created - "AWT-Event Queue". When my program finishes, this thread stays alive and I think it causes some problems I have experienced lately.
Is there any possibility to "kill" this thread without using System.exit() (I can't use it for some specific reasons)All threads are kept alive by the JVM session. When you use System.exit(int) you kill the current session, thus killing all threads. I'm not sure, though...
What you could do, to make sure all threads die, is to make ever thread you start member of a thread group. When you want to exit you app, you kill all the threads in the thread group before exit.
A small example:
//Should be declared somewhere
static ThreadGroup threadGroup = new ThreadGroup("My ThreadGroup");
class MyClass extends Thread {
super(threadGroup, "MyThread");
Thread thread = new Thread(threadGroup, "MySecondThread");
void exit() {
//Deprecated, seek alternative
threadGroup.stop();
System.exit(0);
} -
How to terminate a java thread from c++ code?
Hi,
I made a screensaver which loads a jvm, forks a thread running a java class, wait until user's action(i.e. mouse move/click keyboard input), then terminate the java thread.
However, I met a problem, How to terminate a running java thread from c++ code?
Here is my code, but it does not work: (even after the terminate is called, jvm throws an error)
JNIEnv* env;
JavaVM* jvm;
HANDLE hThread; //handle for the startThread
unsigned __stdcall startThread(void *arg)
jclass cls;
jmethodID mainId;
jint res;
int threadNum = (int)arg;
res = jvm->AttachCurrentThread((void**)&env, NULL);
cls = env->FindClass( MAIN_CLASS);
mainId = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
// setup the parameters to pass to main()
jstring str;
jobjectArray args;
int i=0;
args = env->NewObjectArray(1, env->FindClass("java/lang/String"), 0); //only one input parameters
str = env->NewStringUTF("localhost");
env->SetObjectArrayElement(args, 0, str);
env->CallStaticVoidMethod(cls, mainId, args); // call main()
if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
return TRUE;
Here is the main method:
First create the jvm and load the thread. then tries to terminate the thread, but failed here
switch (msg)
{ case WM_CREATE:
JavaVMOption options[NUMBEROFOPTIONS];
JavaVMInitArgs vmargs;
jint rc;
vmargs.version = JNI_VERSION_1_4; /* version 1.4 */
vmargs.options = options;
vmargs.nOptions = NUMBEROFOPTIONS;
vmargs.ignoreUnrecognized = JNI_FALSE;
rc=JNI_CreateJavaVM( &jvm, (void **)&env, &vmargs ); /* create JVM */
/* We pass the thread number as the argument to the invoked thread */
unsigned int threadId = -1;
// to initialize a thread-safe C runtime library
hThread = (HANDLE)_beginthreadex(NULL, 0, &startThread, NULL, 0, &threadId );
} break;
case (WM_DESTROY):
CloseHandle( hThread );
jvm->DestroyJavaVM(); /* kill JVM */
Debug("Destroy Java Virtual Machine\n");
}break;
Note, after the thread "startThread" runs, it has an infinite loop and will not terminate, until the user clicks to terminate.(I didn't call jvm->DetachCurrentThread();_endthreadex(0); in the "startThread" because the java thread will not terminate by itself)
Thus, the only way to terminate the thread is to call closehandle(hthread) in the main thread, which may cause jvm to throw an error.
Do you know how to terminate a java thread safely???
Thanks a lotAssuming that your java thread is in a loop of some kind. Such as
int i=1; /* I tried using a boolean, I just could not get my C++ env, to change this value. So i decided to use an int */
run {
while(i)
isdfjsdfj
void seti()
i=0
So, B/4, i call destroyVM in my C++ code, i call this seti(). so the loop terminates, therefore my thread finishes executing and ends.
Hope this helps
tola. -
Hello,
How can I kill a Swing thread? For example the program below would never exit. Sure you can type System.exit(0) which would exit anything, but what are the other solutions?
I tried putting chooser = null;
System.gc();in the end of main() but this won’t work.
To formulate my question better - how would I kill a thread I do not have complete command of (i.e. I can’t do usual do/while thread stopping)?
Big thanks in advance.
public class SwingLiveTest{
public static void main(String[] args) {
JFileChooser chooser = new JFileChooser();
if (chooser.showSaveDialog(null) != JFileChooser.APPROVE_OPTION) return;
File file = chooser.getSelectedFile();
System.out.println(file);
/*chooser = null;
System.gc(); */You can't kill any Thread like this, at least not those where there is no method to call.
It is possible to cause a Thread to stop, call the stop method, however this is not the recommended approach for very good reasons (Read the JavaDocs for java.lang.Thread.stop() method to see why.
It should be possible, once you have obtained a reference to the Thread you wish to stop, to call interrupt() on that Thread.
This is not guaranteed to work because you are dependent on the implementation of the Thread class you are manipulating or the Runnable that the Thread instance is currently running.
There may be classes in the com.sun packages which can aid you with this problem, this is where Sun usually sticks undocumented implementation however these packages are not officially supported and there is no guarantee that classes you use in one VM are available in another, in particular when the VM you are using is not a Sun VM. -
How to Kill a Stored Procedure?
Sombody knows how to kill a stored procedure from another stored peocedure?
I wanna know if exists a way to use therads in sored procedures.
Best Regards1) You can't. Unless you code a mechanism yourself, but then since
you can't thread Procedure calls that wouldn't help much.
2) You can try using DBMS_JOB, if your not in a hurry to have your procedure finish.
This is PL/SQL, not Java. You have to imagine all you get is a single thread running
static methods. You can get away from everything being static by using the CREATE TYPE
OBJECT stuff. but that doesn't help you thread your processes.
I hope someone else can give you a different answer,
Eric Kamradt -
How to kill JVM in browser?
Hello
I'm developed an applet if user got out from paget and return befor jvm has cloased, the applet is not play as well
I want to kill the JVM in browser
Thanks.how does one write a thread to a file? if you kill the JVM, that is if you shut tomcat down, all threads within it die. you have no choice about this
I think you've mis-diagnosed your problem -
1)What do u mean by a dead thread????
2)Is it possible to restart a dead???
I think we can never kill a thread....... i think the underlying OS plays a vital role in this aspect
Please correct me if im wrongHi This is Kranthi .Ca u be clear reg this concept of
dead thread.One last time:
A thread is considered dead if it has been started and since terminated. A thread terminates after its run() method completes either normally or abruptly. In the abrupt case the uncaughtExceptionHandler code has to be executed before the thread truly terminates.
The Thread isAlive() method returns true if the thread has been started and has not yet temrinated. So it can't distinguish between a dead thread and a "new" one that hasn't been started yet.
The Thread getState() method will return TERMINATED for a dead thread. -
Hi,
The question is: How can we kill a Thread that we don't have any reference to it.
I have a "closed" jar which launches two threads that don't die after the function execution. I cannot kill the JVM by invoking System.exit[0].
Is there any possibility to access the Threads that are running in memory and kill them? If so how?Hi ChuckBing,
Thanks for replying. Well, actually I think you missunderstood the problem. Let me give you an exemple:
public class Test {
public static void main(String[] args) {
(new DontKillThisAnonymousThread()).start();
(new KillMeThreadLauncherThread()).start();
class DontKillThisAnonymousThread extends Thread{
public void run(){
try{
while(true){
System.out.println("I want to live forever...");
//From this single line of execution
//how can I find another thread (an anonymous one) and kill it.
sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
class KillMeThreadLauncherThread extends Thread{
public void run(){
(new KILL_ME_AnonymousThread()).start();
class KILL_ME_AnonymousThread extends Thread{
public void run(){
try{
while(true){
System.out.println("Are you able to kill me without killing the JVM...");
sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}Thanks
Maybe you are looking for
-
Hyperlink to SSRS Report throws JavaScript Error
Hoping this is the right place to get help here. I'm not sure if this is an error in ASP.NET, SQL Server 2005 Reporting Services or some compatibility issue between VS 2010 and VS 2013 Express for Web. I have an ASP.NET web form written in VS 2010 /
-
oracle apps using forms is it possible refresh,clear form or exit from one form to another form using button, becoz i am using 2 forms called Form A and Form B. Form A is main form and calling Form B by calling function fnd_function.execute(FUNCTION_
-
How to retrieve iMessages from while it was switched off?
Hi, so I reset my IPhone 5 and restored from a backup about 2 weeks ago. I was asked by a friend today why I wasn't responding to his text messages and so I checked and iMessages had been switched off ever since I reset my phone. However, people who
-
2011 Imac running lion. I have the preferences set to automatically log me in as I am the only user. On start up, sometimes it automatically starts up and logs in, sometimes I get the login screen asking for my password. If I supply the password lion
-
Hi, Does anyone know what is the best way to store prices in MySQL, and then how to format it on the output page. Thanks in advance