In general, multi-threaded concurrency and conditional breakpoint debugging are difficult to accomplish, perhaps this article will give you a friendly way to debug. Let you in the multithreaded development process debugging more targeted.

We’ll learn by example. Here, I wrote a multithreaded program to calculate the math problem: 100! + 100000! . So 100 factorial plus 100,000 factorial.

If you’re not good at math, 100 factorial is: 1 * 2 * 3 *… * 100 =? , short for 100!

import java.math.BigInteger; Public class MathProblemSolver {// Open two threads public static void main(String arg[]){// The first thread counts 100! FactorialCalculatingThread thread1 = new FactorialCalculatingThread(100); // The second thread counts 100000! FactorialCalculatingThread thread2 = new FactorialCalculatingThread(100000); thread1.setName("Thread 1"); thread2.setName("Thread 2"); thread1.start(); thread2.start(); try { thread1.join(); // thread Jion, so that the main thread does not execute thread2.join() further until both "thread 1" and "thread2" return a result; } catch (InterruptedException e) { e.printStackTrace(); } BigInteger result = thread1.getResult().add(thread2.getResult()); System.out.println(" add the results of two threads to equal: "+ result); } / / for factorial calculation of Thread class private static class FactorialCalculatingThread extends Thread {private BigInteger result = BigInteger.ONE; private long num; public FactorialCalculatingThread(long num) { this.num = num; } @override public void run() {system.out.println (thread.currentThread ().getName() + num); factorialCalc(num); System.out.println(thread.currentThread ().getName() + "execute done "); } public void factorialCalc(long num) {BigInteger f = new BigInteger("1"); for (int i = 2; i <= num; i++) f = f.multiply(BigInteger.valueOf(i)); result = f; } public BigInteger getResult() { return result; }}}Copy the code

Code explanation above

  • Start two threads and “Thread 1” calculates (100!). And “Thread 2” (100000!)
  • Start two threads in the main() method and callthread1.join()andthread2.join()So that the main thread does not execute further until both thread 1 and Thread 2 return a result.
  • Finally, add the calculation results of the two threads, get100! + 100000!

Let’s use the IntelliJ IDEA tool to debug this multi-threaded code.

Frames and Thread panels

The Frames panel of the debug tool window contains a drop-down menu. It focuses on threads that are suspended due to breakpoints and displays call stack information for those threads. In the figure below, where the breakpoint is in the main() method, Frame shows us the call stack for the main thread.

If you want to examine the call stack of another thread, you can select from the drop-down list.

The Thread panel displays all threads that are currently active. Referring to the code above, I add a breakpoint in thread1.join(). When the application pauses at this breakpoint, we should see at least three threads in this pane – “main,” “Thread 1,” and “Thread 2” (see the screenshot below). You can double-click on each thread to see its call stack.

Conditional breakpoint – Suspends only threads that meet the condition

Suppose I’m fixing a bug in the program, and I only need to pause execution when “Thread 2” starts running. This shows that I need in FactorialCalculatingThread run () method on the first line of the add a breakpoint. Because the two threads we started were using the same code, we ran into a problem – all threads using the code that encountered breakpoints would be suspended, including the application’s “Thread 1” and “Thread 2.” I don’t want both threads to pause. What should I do?

We can use the conditional breakpoint function. After you add a breakpoint, right-click it, select “suspend” and select “Thread.” We then add the condition currentThread().getName().equals(“Thread 2”), as shown in the screenshot below. This condition ensures that the debugger suspends the current Thread only if its name is “Thread 2” :

Now run the debugger, and when the application is paused, only “Thread 2” is paused. You can verify that Thread 1 is executed and not suspended by performing the following steps:

1. On the console, you can verify that Thread 1 runs and exits using logs.

2. In the “Thread” panel, you can see that the “Thread 1” is no longer running.

The way conditional breakpoints are configured may vary from IDE version to IDE version. But the key idea is to recognize these features and use them.

Welcome to my blog, where there are many fine collections

  • This article is reprinted with a credit (must be accompanied by a link, not only the text) : Antetokounmpo blog.

Feel helpful to you, help me like, share! Your support is my inexhaustible creative power! . In addition, the author recently a period of time output as follows boutique content, looking forward to your attention.

  • Spring Boot2.0 by Hand
  • Spring Security- JWT-OAUTH2
  • RBAC Authority Management System for Actual Combat Front-end and Back-end Separation
  • “Actual SpringCloud Micro-service from Bronze to King”
  • VUE Series