switching between threads in Intellij Idea

后端 未结 2 1426
谎友^
谎友^ 2020-12-16 20:44

How to switch between threads of a suspended program?

or Any tutorial on multi-threaded debugging with Intellij Idea describing basic features - suspend, resume, sw

2条回答
  •  离开以前
    2020-12-16 21:31

    Trick is to set breakpoint suspend policy to - Thread. View breakpoint properties (right-click on breakpoint)

    breakpoint properties - suspend policy

    Once done threads will hit breakpoint and block, now active thread can be switched to check race conditions/deadlocks.

    switching between threads

    Following code snippet for creating deadlock:

    public static void main(String args[]) {
        Thread thread1 = new Thread(null, new MyThread(obj1, obj2), "Thread-1");
        Thread thread2 = new Thread(null, new MyThread(obj2, obj1), "Thread-2");
        thread1.start();
        thread2.start();
    }
    class MyThread implements Runnable {
        private Object obj1;
        private Object obj2;
    
        MyThread(Object obj1, Object obj2) {
            this.obj1 = obj1;
            this.obj2 = obj2;
        }
    
        @Override
        public void run() {
            System.out.println("Acquiring locks");
            synchronized (obj1){
                System.out.println("Acquired 1st lock");
                synchronized (obj2){
                    System.out.println("Acquired 2nd lock");
                }
                System.out.println("Released 2nd lock");
            }
            System.out.println("Released 1st lock");
        }
    }
    

提交回复
热议问题