Suspending the JVM for a limited time
How can I pause the JVM execution for a configurable amount of time, similar to what happens in a full, sequential, garbage collection? I want to check for some edge cases, but I'm having a hard time creating the exact pauses I need with manual garbage + creation System.gc()
.
I am trying to freeze all threads in an application to simulate a situation where the application stops responding and then resumes execution. The app is part of a cluster and I am trying to debug some send / merge / re-merge issues.
a source to share
When debugging from Eclipse you can support all threads, I think other debuggers can do that as well. Therefore, you need to start the server with JVM debug options and a remote connection to it.
In my case (JBoss startup) I modify the startup script by adding this line:
set JAVA_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n %JAVA_OPTS%
Then in Eclipse do -> Debug Configurations -> Remove Java Application -> New
Usually you need to provide the hostname and port.
a source to share
I think you probably want something less invasive, but one approach is for the control thread to acquire the lock and then all other threads will try to acquire that lock as well. Then curtail the control flow for the time you need before giving it up. A slightly less awkward version of this would be to use a semaphore with one permission per thread and then get the control thread to get all of them before all other threads try to get the permission.
As I said, quite aggressive, you'll have to hack this into your code for each thread.
Another approach would be to run your code under a debugger and manually pause each thread.
a source to share
JVM substitution can be done from the command line using jdb :
jdb -attach 8787
Initializing jdb ...
> suspend
All threads suspended.
> resume
All threads resumed.
> exit
But this requires it to start with -Xdebug ...
There is also a generic way to suspend and resume any process on Linux / Unix:
kill -SIGSTOP PID
kill -SIGCONT PID
a source to share