File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to Stop Java threads which are Running Infinitely Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to Stop Java threads which are Running Infinitely" Watch "How to Stop Java threads which are Running Infinitely" New topic
Author

How to Stop Java threads which are Running Infinitely

Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Hi

I have a Java program that is developed using threads. Once we start the java program using “java classname” that will infinitely looking into the IBM MQ. Currently we are killing the process to stop the service. Someone Please assist me to stop the javathread using the command line itself.

Or I can write another java program to stop or interrupt the thread which is running infinitely.

Please assist me.

Srikanth Nalam
Greenhorn

Joined: Feb 23, 2010
Posts: 20
Hi Meet,

If you are using windows OS, you can use (PS tools) pskill.exe to kill a process.

Google to download and usage.


Thanks & Regards,
Srikanth Nalam.
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Sorry It's Unix Server
Peter van de Riet
Ranch Hand

Joined: Apr 09, 2004
Posts: 112

If you can put yourself messages on the queue, you can add a "STOP" message.
Or you can add a "control queue" where you send messages to, this can also be used to send other controls, like for instance loglevel.


Each number system has exactly 10 different digits.
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492


From another class I tried setting the flag = true.. But no use...Please assit me
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

That's because the other program has another JVM instance, so another instance of the class. The two "flags" are unrelated.

Search around for interprocess communication. Usual ways are using sockets, files, mapped memory, ..

Using a file it could be easy, but I don't know if the following code will run into synchronization problems. That's up to the file system probably.
Original process:
Stopping process:
On Unix / Linux, /var/run/XXX.pid (replace XXX with your application name) is a file used regularly, although it usually contains the process ID of the actual process.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Manish Singh
Ranch Hand

Joined: Jan 26, 2007
Posts: 160
why depending on the existence of the file.

use the property like

keepRunning=true

in some property file
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

That would work as well but it would require the file to be actually read. It may even be absent. If that's the only content then checking for the file's presence is faster. Your solution is more scalable though; if you get more properties you can simply add them to the file.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to Stop Java threads which are Running Infinitely
 
Similar Threads
Java Daemons
Executing a Jar file
Java Threads
Java program terminates
Verifying if a thread is alive