As the title says above I am wondering if there is anyway to pause a java application or the JVM that is running as a daemon on a server and then restart it at a later point. I have an application that runs long processes that can take up to a week to finish making it very hard to perform any maintenance on other parts of the system.
-
`Thread.sleep(amount)` – Malik Brahimi May 12 '15 at 14:22
-
You can do it externally via the OS, if that's what you wanted. – keyser May 12 '15 at 14:23
-
Almost all OSes are able to pause processes (*Suspending*). Make sure that you don't restart the OS as this will invalidate and close all handles as well as stop the application. Also, try not to edit any opened files or folders. – GiantTree May 12 '15 at 14:24
-
Yes externally is fine I just need to be able to pause the execution of the whole program which is running in many threads so I don't think thread.sleep will work. Also I would prefer to be able to pause it indefinateley and then restart it later when I am ready. – Jake C. May 12 '15 at 14:24
-
1Then the answer is OS-dependent, e.g. ctrl-z on linux. – keyser May 12 '15 at 14:25
-
The best option would be to save the state of your program to a file. Then you can resume it at some later time. What kind of processes are you running? – Tesseract May 12 '15 at 14:34
-
That's one of the problems it is running this external program called radiance from java and is being driven by 6 different RabbitMQ queues, so it is probably to complex to save the state to a file or just use thread.sleep. – Jake C. May 12 '15 at 14:37
-
Then you might want to check out something like [CryoPID](http://stackoverflow.com/a/2137808/2058898), which seems to do what you want to do too – michaeln May 12 '15 at 14:40
-
I'm sure it's possible somehow to save the state. Does radiance always run for a very long time? If it only runs for short intervals it shouldn't pose a problem. And what applications are sending data through those RabbitMQ queues? You would have to send them a message telling them to pause/shut down. – Tesseract May 12 '15 at 14:46
-
Radiance has very high variability in how long it will run, it could be a couple seconds to a couple weeks. Yes, sending a pause/shut down message is one avenue I am exploring. Just trying to get a sense of what can and has been done in the past. – Jake C. May 12 '15 at 14:49
-
So just to clarify things - do you need the ability to reboot the system while your application is paused? If that's not the case and you are fine with leaving it in memory, it's sufficient to suspend it as others have suggested. (e.g. with ctrl-z on linux) – Tesseract May 12 '15 at 14:55
-
No need to reboot just suspend the running processes. – Jake C. May 12 '15 at 14:56
-
Maybe all you need to do is suspend radiance? Your main program will then pause automatically as it waits for radiance to return a result. – Tesseract May 12 '15 at 14:59
-
I think pausing the main thread may be the solution I am after, I create the radiance processes as a sub-process in java so I think by pausing the main process everything else will pause as well. – Jake C. May 12 '15 at 15:35
4 Answers
If the code is under your control then you could consider some internal mechanism in the code to pause your threads. Something like a PausableThread may be worth trying.
The benefit of this would be that you could control what state the machine is in when you pause. You could ensure you only check for pause when files are closed or database queries are complete.

- 1
- 1

- 64,482
- 16
- 119
- 213
You might want to check out java.io.Serializable
which allows you to save objects from the runtime and even stop the application completely. You then can restart the application at a later point.

- 1,032
- 17
- 33
This can be done in various ways, but the approach will largely be determined by what your process does. e.g. if its doing very large number of similar small jobs, or small units or work:
while(PendingJobs > 0){
work(job);
removeFromPendingQueue(job);
//implement a pause signal check
while(pauseFlag){
Thread.sleep(10000);
}
}
In such a scenario you can just set the pause flag through some url call, or blocking on IO (e.g. presence of a lock file etc). Thus the process will complete the current job and pause before taking up the next job, while the pauseFlag is set true.
However if your process is performing one single lengthy job, that itself runs over multiple days / weeks. There is tons you got to to worry about: e.g. Transaction management, connection timeouts, preserving state and resuming right from where you left etc.
Either case the application will have to enhanced and designed as such.

- 1,241
- 11
- 18
The best solution would be to use Thread.yield() frequently, and lower thread priority Thread.setPriority(). What yield() does is saying "i give up my time slot of cpu if some other process needs it" so you'll be able to use the machine even if the process is still running, it could impact the performance though.
If you want to pause completely the processing whenever you want a simple condition with a boolean will do, every thread lock and check if the a boolean paused is true once every N iterations, if it is they wait for the condition, if it's not they unlock and continue as always. On the controller thread you just lock, set paused to true, and unlock, when you want to resume the processing you can just lock, set paused to false, use a Condition.signalAll(), and unlock.
Also you could try to change priorities with nice, even if usually they don't change much (only cpu usage scheduling, they don't have much impact on i/o scheduling)
If you want to stop completely the process and resume it, the best way is to serialize all your context with a serialization library (there is plenty of libraries out there), write it to disk and deserialize when you restart the application.

- 316
- 2
- 9