2

Is there a way to suspend all business-logic execution threads of Java process when execution hits some method?

If it is not possible via external configuration is there any method call to embedd into application to achieve desired result?

gavenkoa
  • 45,285
  • 19
  • 251
  • 303
  • You might use Object.wait, or Thread.sleep(ms). Each work on the current executing thread. Oh wait, I suspect you cannot modify the source code, right? – LppEdd Jul 17 '18 at 12:54
  • You mean you want to wait before attaching a debugger, instead of having the debugger attached and waiting for it to hit a breakpoint? – Kayaman Jul 17 '18 at 12:56
  • I cannot run application from debugger/profiler but can attach to process. `Thread.sleep` is not productive solution. – gavenkoa Jul 17 '18 at 13:15
  • Then attach to a process. Set a breakpoint and you're ready to go. No need for any weird tricks you were thinking about. – Kayaman Jul 17 '18 at 13:20

2 Answers2

7

According to how to profile application startup with visualvm I can start application with

-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y

to stop execution on app startup.

May be it would be possible to set breakpoint and move to interesting app state and then attach with profiler and start app again.

Another option is to start with Java agent: https://visualvm.github.io/startupprofiler.html

gavenkoa
  • 45,285
  • 19
  • 251
  • 303
2

One solution is the place this code right before the method you want to debug: System.out.println("Press Any Key To Continue..."); new java.util.Scanner(System.in).nextLine(); This waits until you press enter, so you have time to attach your debugger.

Another method is to use the IDE Netbeans, which contains a debugger and allows you to set breakpoints anywhere.

Alex Lin
  • 94
  • 2
  • I can start Java process with `-agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=y`. But can other tools attach to this process for instrumentation? – gavenkoa Jul 17 '18 at 13:13
  • @gavenkoa that's the whole point. Have you tried connecting there with a debugger? – Kayaman Jul 17 '18 at 13:22