简体   繁体   中英

How to kill a particular process in java

Hi all i Have two process in my java application as below.

Process p1=some process;
Process p2=some process;

At the first time it starts its working perfectly.But when i reload the application the older process is also running so i just want to kill the older process if its executing.I know that we can kill a process by using its process id.But how can i get the process id of this p1 and p2.Any idea?.

Catch the reload event in your application and terminate the child process before you exit.

If that doesn't work, you probably have a bug in your design. Try to fix that bug. Really. I mean it.

A workaround is to write the PID into a text file. When you start, read the text file, check if this is the correct process and then kill it.

Without the check, you might be killing a perfectly valid process which was created by someone else. My reasoning is like so: You can't kill the process when you reload. That means you can't delete the text file either. So the PID file will always exist. Eventually, you will kill the wrong process. That will happen once per year. It will happen the first time six months after you left/changed job/etc. Nobody will understand what is going on because it's almost impossible to reproduce.

As others have said, it would be better to terminate the process in a more natural way.

However, if you do need to actually forcibly terminate it, then you could call the destroy() method on the Process object, whenever you don't care about it running anymore.

Also, do you really need to run the code as a Process? There are generally better ways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM