• java_Thread


    //饿汉式
    class Single{
    private final static Single s = new Single();
    public static Single getInstance(){
    retrun s;
      }
    }
    //懒汉式
    class Single{
    private static Single s = null;
    public static Single getInstance(){
             if(s==null)
    s=new Single();
    return s
      }
    }


    //死锁案例
    class Dead implements Runnable{
    private boolean b=false;
    Dead(boolean b){
    this.b=b;
       }
    public void run(){
    while(true){
    if(b){
    synchronized(Locks.locka){
              S.O.P(Thread.currentThread().getName());
    synchronized(Locks.lockb){
     S.O.P(Thread.currentThread().getName());
               }
              }
    }
    else{
    synchronized(Locks.lockb){
    S.O.P(Thread.currentThread().getName());
    aynchronized(Locks.locka){
    S.O.P(Thread.currentThread().getName());
    }
              }
    }
          }
        }
    }
    //创建锁
    class Locks{
    public static Object locka = new Object();
            public static Object lockb = new Object();
    }


    class DeadLock{
    public static void main(String[] args){
    Dead d1=new Dead(true);
    Dead d2=new Dead(false);
    Thread t1 = new Thread(d1);
    Thread t2 = new Thread(d2);
    t1.start();
    t2.start();
      }
    }

  • 相关阅读:
    javascript-事件
    javascript-数组简单的认识
    javascript-dom操作
    JavaScript入门
    python语言程序设计?
    4,14
    python第二周。2019.4.13
    python第二周
    python语言程序设计9
    python语言程序设计8
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3104889.html
Copyright © 2020-2023  润新知