JAvaデッドロックの実装

4030 ワード

package        ;

class DThread implements Runnable

{

    private Object o1=null;

    private Object o2=null;

    public DThread(Object o1,Object o2)

    {

        this.o1=o1;

        this.o2=o2;

    }



    @Override

    public void run() {

        // TODO Auto-generated method stub

              synchronized(o1)

              {

                  try {

                    Thread.sleep(1000);

                } catch (InterruptedException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                }

                  synchronized(o2)

                  {

                      System.out.println("      ,   ,  "+Thread.currentThread().getId());

                  }

                  

                  

              }

    }

    

    

    }



public class DeadLockTest {

    public static void main(String args[])

    {

        System.out.println("    ");

        Object o1=new Object();

        Object o2=new Object();

        //  10   ,  1,2   。

        for(int i=0;i<100;i++)

        {

            new Thread(new DThread(o1,o2)).start();;

        }

        //  1,0   

        for(int i=0;i<100;i++)

        {

            new Thread(new DThread(o2,o1)).start();;

        }

        for(int i=0;i<100;i++)

        {

            new Thread(new DThread(o1,o2)).start();;

        }

        

        

    }



}