6

In my past life as a C/C++ programmer, it was possible on some platforms & debugger combinations to selectively suspend threads. After hitting a breakpoint, one could issue commands (or click stuff in the GUI) to ice/de-ice (suspend / wake) a thread. Upon further step/next/run/continue commands, suspended threads would not execute any instructions.

Is this possible with any JVM or Java IDE today? I use IntelliJ and I don't see the feature, nor can I find any information via Google.

To clarify:

  1. I am not asking about the thread policy for a breakpoint, e.g, suspend current thread or all threads. I am aware of that JVM debugger feature.
  2. The dev environment is vanilla: I use IntelliJ Java IDE, and I have access to the source code in question.

Finally, I realise the "Java debugger" is a somewhat vague term. AFAIK, Sun published a debug-on-the-wire format that debuggers use to communicate with the JVM. Perhaps each JVM (IBM vs Sun vs IcedTea vs ???) supports different debug capabilities, but I am only familiar with the Sun/Oracle JVM.

kevinarpe
  • 20,319
  • 26
  • 127
  • 154
  • Hi kevin, do you have control over the code you are running, and is it just one specific point? since if you are prepared to modify the code you could use a `boolean threadSuspended` and `if (!threadSuspended)` pattern, and set that boolean value manually in the debugger. Sorry that I can't see a better answer. – vikingsteve Oct 01 '14 at 08:45
  • 1
    Some relevant discussion on deprecation of programmatic suspend / resume is here: http://docs.oracle.com/javase/8/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html – vikingsteve Oct 01 '14 at 08:46
  • Eclipse has a feature called conditional breakpoints where some arbitrary java code is evaluated at hitting the breakpoint and before suspending the current thread. So you could execute some code there that additionally suspends other threads. Don't know if IntelliJ has a comparable feature. – SpaceTrucker Oct 01 '14 at 08:52
  • Maybe this isn't possible. I assume JVM uses pthreads on all platforms that support it. If so, see: http://stackoverflow.com/a/9408451/257299 – kevinarpe Oct 01 '14 at 09:01
  • @vikingsteve: That is a good workaround. I suggest you add as a separate answer. – kevinarpe May 16 '16 at 08:05

4 Answers4

3

You can definitely suspend a single thread in jdb (see below). I love IntelliJ as a Java editor. On the other side its debugger is not terrible, but close.

Initializing jdb ...
> run
Nothing suspended.
> threads
Group system:
  (java.lang.ref.Reference$ReferenceHandler)0x141 Reference Handler cond. waiting
  (java.lang.ref.Finalizer$FinalizerThread)0x140  Finalizer         cond. waiting
  (java.lang.Thread)0x13f                         Signal Dispatcher running
  (java.lang.Thread)0x401                         Java2D Disposer   cond. waiting
  (java.lang.Thread)0x804                         TimerQueue        cond. waiting
Group main:
  (java.lang.Thread)0x322                         AWT-AppKit        running
  (java.lang.Thread)0x323                         AWT-Shutdown      cond. waiting
  (java.awt.EventDispatchThread)0x5ec             AWT-EventQueue-0  cond. waiting
  (java.util.TimerThread)0x750                    Timer-0           cond. waiting
  (java.lang.Thread)0x7fd                         DestroyJavaVM     running
> suspend 0x5ec             
> threads
Group system:
  (java.lang.ref.Reference$ReferenceHandler)0x141 Reference Handler cond. waiting
  (java.lang.ref.Finalizer$FinalizerThread)0x140  Finalizer         cond. waiting
  (java.lang.Thread)0x13f                         Signal Dispatcher running
  (java.lang.Thread)0x401                         Java2D Disposer   cond. waiting
  (java.lang.Thread)0x804                         TimerQueue        cond. waiting
Group main:
  (java.lang.Thread)0x322                         AWT-AppKit        running
  (java.lang.Thread)0x323                         AWT-Shutdown      cond. waiting
  (java.awt.EventDispatchThread)0x5ec             AWT-EventQueue-0  waiting in a monitor
  (java.util.TimerThread)0x750                    Timer-0           cond. waiting
  (java.lang.Thread)0x7fd                         DestroyJavaVM     running
> where 0x5ec
  [1] java.lang.Object.wait (native method)
  [2] java.lang.Object.wait (Object.java:485)
  [3] java.awt.EventQueue.getNextEvent (EventQueue.java:558)
  [4] java.awt.EventDispatchThread.pumpOneEventForFilters (EventDispatchThread.java:263)
  [5] java.awt.EventDispatchThread.pumpEventsForFilter (EventDispatchThread.java:211)
  [6] java.awt.EventDispatchThread.pumpEventsForHierarchy (EventDispatchThread.java:201)
  [7] java.awt.EventDispatchThread.pumpEvents (EventDispatchThread.java:196)
  [8] java.awt.EventDispatchThread.pumpEvents (EventDispatchThread.java:188)
  [9] java.awt.EventDispatchThread.run (EventDispatchThread.java:122)
AWT-EventQueue-0[1] where 0x322
Current thread isn't suspended.
AWT-AppKit[1] resume 0x5ec
AWT-AppKit[1] where 0x5ec
Current thread isn't suspended.
AWT-EventQueue-0[1]
jdb
  • 4,419
  • 21
  • 21
  • 1
    My head almost just exploded. Outstanding information! The key to this answer: It proves the JVM debug wire protocol supports this feature. Now to file a request with JetBrains... – kevinarpe Oct 05 '14 at 11:52
  • 1
    Readers: Please upvote the IntelliJ feature request here: http://youtrack.jetbrains.com/issue/IDEA-79921 – kevinarpe Oct 06 '14 at 10:04
2

IntelliJ IDEA has it now (I'm looking at v15). The Debug window has a Threads panel, that allows suspending and resuming any thread.

https://www.jetbrains.com/idea/help/debug-tool-window-threads.html

Screenshot of Threads panel

OrangeDog
  • 36,653
  • 12
  • 122
  • 207
1

Eclipse can do it at least with the Oracle VM but I guess it's possible with all of them. Try to select a thread in the debugger (there should be a view with all the running threads). Then the suspend button should only suspend this thread instead of all of them.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
1

Do you have control over the code you are running, and is it just one specific point?

Since if you are prepared to modify the code you could use a boolean threadSuspended and if (!threadSuspended) pattern, and set that boolean value manually in the debugger.

It's a practical workaround - not completely the feature you are after but it might do the job ;)

vikingsteve
  • 38,481
  • 23
  • 112
  • 156