Question About Deadlock Situation in Java

前端 未结 6 2266
鱼传尺愫
鱼传尺愫 2020-12-08 12:06

I\'m learning about deadlocks in Java, and there\'s this sample code from Sun\'s official tutorial:

Alphonse and Gaston are friends, and great belie

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 12:37

    To add to simonn, et al.,

    If you would like to visualize this, compile and run the program and generate a thread dump of the running program. You can do this by typing a Ctrl-Break to a Windows console or issueing a kill -QUIT [pid] command to a *nix system. What this will provide you is a list of all of the Threads running in your system and where they are either running or waiting, as well as the monitors the threads are either locking on or waiting to lock on.

    If you change your Thread names in their constructors, you will have an easier time finding them in the full thread dump:

       new Thread(new Runnable() {
            public void run() { alphonse.bow(gaston); }
        }, "Alphonse").start();
        new Thread(new Runnable() {
            public void run() { gaston.bow(alphonse); }
        }, "Gaston").start();
    

提交回复
热议问题