//饿汉式
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();
}
}