黑马程序员_多线程的死锁和多线程下的单例设计模式

来源:互联网 发布:xp系统网络共享密码 编辑:程序博客网 时间:2024/06/03 08:15

多线程的单例设计模式

//饿汉式class Single{    private static final Single s=new Single();    private Single(){}    public static Single getInstance(){        return s;    }}//懒汉式class Single2{    private static Single2 s=null;    private Single2(){}    public static Single2 getInstace2(){        if(s==null){            synchronized (Single2.class) {//使用这种加一个判断条件的写法,可以解决效率低的问题,                                                                //也可以解决安全性问题            s=new Single2();            }            }        return s;    }}public class Thread21_1 {        public static void main(String[] args) {        }}

多线程的死锁

死锁的常见情景之一:同步的嵌套:有锁A和锁B。需要A,B同时才能执行好一段代码。在多线程的情况下。就2个线程。当线程1拿住A锁的同时想拿B锁。但是,这个时候,线程2拿住B锁想拿A锁。他们的代码都没有做完,彼此都不放自己手上的锁。这个时候,线程1和线程2都不能做自己想做的事情,就死锁了。

实例一

class  Ticket implements Runnable{    private  int num=400;    Object obj=new Object();    public boolean flag=true;    public  void run(){    //  System.out.println("this:"+this);        if(flag){        while(true){            synchronized(obj){                    show();                }        }        }else{            while(true){            show();        }     }    }    public synchronized void show(){        synchronized(obj){        if(num>0){            try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}            System.out.println(Thread.currentThread().getName()+"....sale...."+ num--);            }        }    }}public class Thread22_1 {    public static void main(String[] args) {            Ticket t=new Ticket();            Thread t1=new Thread(t);            Thread t2=new Thread(t);             t1.start();             try {                Thread.sleep(10);            } catch (InterruptedException e) {                e.printStackTrace();            }             t.flag=false;             t2.start();    }}

实例二

    class Test implements Runnable{    private boolean flag;    Test(boolean flag){        this.flag=flag;    }    public void run(){        if(flag){            while(true)            synchronized (MyLock.locka) {                System.out.println(Thread.currentThread().getName()+"..if   locka...");                synchronized(MyLock.lockb){                    System.out.println(Thread.currentThread().getName()+"..if   lockb...");                }            }        }else{            while(true)            synchronized (MyLock.lockb) {                System.out.println(Thread.currentThread().getName()+"..else   lockb...");                synchronized(MyLock.locka){                    System.out.println(Thread.currentThread().getName()+"..else  locka...");                }            }        }    }}class MyLock{    public static final Object locka=new Object();    public static final Object lockb=new Object();}public class Thread22_2 {    public static void main(String[] args) {        Test a=new Test(true);        Test b=new Test(false);        Thread t1=new Thread(a);        Thread t2=new Thread(b);        t1.start();        t2.start();    }}
0 0
原创粉丝点击