Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Kill the thread

Status
Not open for further replies.

WebGoat

MIS
Jul 16, 2005
85
US
My thread class has a run method

Code:
public void run()
{
Process p=Runtime.exec(runExternalProcess);
p.waitFor();
}

i want to kill the thread after 20 sec. how is that possible
 
You want to interrupt a class that extends Thread or you want to kill the process spawned by Runtime.exec() ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
You want to interrupt a class that extends Thread or you want to kill the process spawned by Runtime.exec() ?

>You want to interrupt a class that extends Thread.

Yes. is it possible in this scenario ?

>you want to kill the process spawned by Runtime.exec() ?

i assumed it can not be done. because , once an external process has been fired we can not have control over it, so i thought this can not be done.

i am very much eager to know if there is way to do it ? is this possible ?




So, actually, i want the both i.e interrupting the thread and also killing the external process which is spawned by Runtime.exec().

thank you
 
You are correct about not being able to kill the spawned process.

To interrupt a thread, override the interrupt() method, and have some flag in your run method that is a class variable :

Code:
void interrupt() {
   keepRunning = false;
}

void run() {
  while (keepRunning) {
     doStuff();
  }
}

Then when you call the interrupt() method on the thread, it will break out of the run() method.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
where do i put my doStuff() ?
Code:
Process p=Runtime.exec(runExternalProcess);
p.waitFor();


if i change your code with my doStuff() . it becomes like this

Code:
void interrupt() {
   keepRunning = false;
}

void run() {
  while (keepRunning) {
        Process p=Runtime.exec(runExternalProcess);
	p.waitFor();
  }
}


so, everytime this while loop will start calling this "Runtime.exec(runExternalProcess)" . but i want to start calling my stuff once and after 20 secs i want to interrupt the thread. so there is a problem because the while loop is initiating the external process in every iteration .
 
I don't quite follow here. Why would you want to spawn a process, and then only wait for 20 seconds ? That seems really wrong to me - if the process is running, then it is running - *exiting* the thread that spawned it isn't going to do anything to the spawned process, and you really shouldn't exit threads until all resources are sorted out (ie your process).

Plus, the final killer here is that p.waitFor() is a blocking call - so you won't be able to interrupt it and exit the thread anyway.

I'd go back to the drawing board and really try to figure out what you want to achieve.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
>Why would you want to spawn a process, and then only wait for 20 seconds ?

i want to run it for 20 secs only. if the external process can do its job within 20 secs then its good , otherwise i dont want to wait for it.




>Plus, the final killer here is that p.waitFor() is a blocking call - so you won't be able to interrupt it and exit the thread anyway.

yes, you are right. i should remove the p.waitFor() method immediately.


but still the same problem , that everytime the same external process will be called because of the while loop . can i stop that ?
 
Is the external process running code written by yourself?

If so, you probably should enhance that code with something which allows you to signal it to die - something like a socket for example. You could then launch it and after 20 seconds fire a datagram packet at it to tell it to die.


Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
If you don't call waitFor() then you won't be able to determine the exit status of the process.

If you don't give a monkies about it, the process or the CPU then forget all the interrupt stuff and just do this :

Code:
Process p = ...
try {
  Thread.sleep(20000);
} catch (Exception e) {
}

But this is really really bad practice.

You should really re-think what you are trying to achieve here.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
>Is the external process running code written by yourself?
NO

Code:
Process p = ...
try {
  Thread.sleep(20000);
} catch (Exception e) {
}

ohh. its good.

thank you
 
That's pretty much like just launching the process, isn't it?

When you wake up from sleep, you've lost the control over it. You can't even know if it's still alive.

Maybe knowing the nature of the external proccess would help, but a first sight I think you could just do the waitFor thingie and launch another waiting thread. When the second one awakes, it can check wether the process has finished and killing the thread if not.

Anyway, it's again the same thing as launching the proccess alone: you won't be able to stop it unles you invoke some king of system dependent command.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top