Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

multithreading - switching between threads in Intellij Idea

How to switch between threads of a suspended program?

or Any tutorial on multi-threaded debugging with Intellij Idea describing basic features - suspend, resume, switch between threads.

very good tutorials/step-by-step guide available for Netbeans: e.g. https://netbeans.org/kb/docs/java/debug-multithreaded.html

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...