1

I am new to the world of multithreading and deploying code on a server. I have a project that has a main thread. This main thread has a thread pool of 10 other threads that "query" different hardware using SNMP. Those threads then do some database stuff and that database goes to a webapp front end. The main thread uses a scheduler to keep the task running for ever and repeats every 15 seconds.

In eclipse I exported a runnable .jar and ran it on a server using "nohup java -jar nameOfJar.jar &". Now to kill this process I use "ps -ef | grep java" to see what PID the nameOfJar.jar is running on and use "kill PIDofNameOfJar" to actually kill the process. I am new to multithreading and am wondering what happens to the threads when I use the kill command. Is any clean up done on them? Or should I have code to deal with this? Or should I not be using the kill command to stop a runnable jar?

I am thinking that since I am killing the main process the other 10 threads will not be terminated if they have been sent out but I'm not sure.

Any help would be appreciated!

1 Answers1

0

There is no other cleanup besides releasing the resources held by JVM (so basicly allocated memory, maybe sockets and file hooks). Any other resouces (like commiting or aborting db transactions) must be done prior that as this wont happen automaticly.

You can hook yourself up into JVM shutdown process via Runtime.addshutdownhook(). This hook will be executed while JVM is closing (not abnormally like due to JVM error) eg. when sigterm is sent to JVM. You can do your cleanup there.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99