• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Stop Java threads which are Running Infinitely  RSS feed

 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Meet,

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

Google to download and usage.

 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry It's Unix Server
 
Peter van de Riet
Ranch Hand
Posts: 112
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


From another class I tried setting the flag = true.. But no use...Please assit me
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manish Singh
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why depending on the existence of the file.

use the property like

keepRunning=true

in some property file
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!