whx20100101

多线程死锁的简单使用:

public class MyLock{
    public static final Object locka=new Object();
    public static final Object lockb=new Object();
    public static final Object lockc=new Object();
    public static final Object lockd=new Object();
}
package day_12_01_Thread;

/**
 * 死锁的情况
 * 
 * @author Administrator
 *
 */
public class TestSynchronized implements Runnable {
    public static void main(String[] args) {
        TestSynchronized s = new TestSynchronized();
        Thread t1 = new Thread(s);
        Thread t2 = new Thread(s);

        t1.start();
        t2.start();
    }

    @Override
    public void run() {
        int i = 0;
        while (true) {
            if (i % 2 == 0) {
                synchronized (MyLock.locka) {
                    System.out.println("if.......locka");
                    synchronized (MyLock.lockb) {
                        System.out.println("if ...........lockb");
                    }
                }
            } else {
                synchronized (MyLock.lockb) {
                    System.out.println("else..........lockb");
                    synchronized (MyLock.locka) {
                        System.out.println("else..........locka");
                    }
                }
            }

            i++;
        }
    }
}

 

分类:

技术点:

相关文章:

  • 2021-09-28
  • 2021-11-23
  • 2018-07-29
  • 2022-01-03
  • 2022-02-27
  • 2021-11-21
猜你喜欢
  • 2022-12-23
  • 2022-02-26
  • 2021-11-04
  • 2021-12-03
  • 2022-12-23
相关资源
相似解决方案