线程 单列设计模式

 

 

这里注意个问题哈:

这样只能确保单线程的情况下不会不同  会只有一个对象

在多线程的情况下就可能会有多个对象

解决措施 使用同步机制

如下

package com.bjsxt.thread.syn;
/**
 * 单例设计模式:确保一个类只有一个对象
 * @author Administrator
 *
 */
public class SynDemo02 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        JvmThread thread1 = new JvmThread(100);
        JvmThread thread2 = new JvmThread(500);
        thread1.start();
        thread2.start();
        
    }

}
class JvmThread extends Thread{
    private long time;
    public JvmThread() {
    }
    public JvmThread(long time) {
        this.time =time;
    }
    @Override
    public void run() {        
        System.out.println(Thread.currentThread().getName()+"-->创建:"+Jvm.getInstance(time));
    }
}


/**
 * 单例设计模式
 * 确保一个类只有一个对象
 * 懒汉式  double checking
 * 1、构造器私有化,避免外部直接创建对象
 * 2、声明一个私有的静态变量
 * 3、创建一个对外的公共的静态方法 访问该变量,如果变量没有对象,创建该对象
 */
class Jvm {
    //声明一个私有的静态变量
    private static Jvm instance =null;    
    //构造器私有化,避免外部直接创建对象
    private Jvm(){
        
    }
    //创建一个对外的公共的静态方法 访问该变量,如果变量没有对象,创建该对象
    public static Jvm getInstance(long time){
        // c d e  -->效率  提供 已经存在对象的访问效率
        if(null==instance){    
            // a b
            synchronized(Jvm.class){
                if(null==instance ){
                    try {
                        Thread.sleep(time); //延时 ,放大错误
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    instance =new Jvm();
                }
            }
      }//a
      return instance;
    }
    
    
    public static Jvm getInstance3(long time){
        //a b c d e  -->效率不高 c  存在对象也需要等待
        synchronized(Jvm.class){
            if(null==instance ){
                try {
                    Thread.sleep(time); //延时 ,放大错误
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                instance =new Jvm();
            }
            return instance;
        }
    }
    
    
    public static synchronized Jvm getInstance2(long time){
        if(null==instance ){
            try {
                Thread.sleep(time); //延时 ,放大错误
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            instance =new Jvm();
        }
        return instance;
    }
    
    
    
    public static Jvm getInstance1(long time){
        if(null==instance ){
            try {
                Thread.sleep(time); //延时 ,放大错误
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            instance =new Jvm();
        }
        return instance;
    }
}

 

 

 

 

 

单实例创建模式:懒汉式 和 饿汉式

package com.bjsxt.thread.syn;
/**
 * 单例创建的方式
 * 1、懒汉式
 * 1)、构造器私有化
 * 2)、声明私有的静态属性
 * 3)、对外提供访问属性的静态方法,确保该对象存在
 * 
 * @author Administrator
 *
 */
public class MyJvm {
    private static MyJvm instance;
    private MyJvm(){
        
    }
    public static MyJvm getInstance (){
        if(null==instance){ //提供效率
            synchronized(MyJvm.class){
                if(null==instance){ //安全
                    instance =new MyJvm();
                }
            }
        }
        return instance;
    }
    

}
/**
 * 饿汉式
   1)、构造器私有化 
 * 2)、声明私有的静态属性,同时创建该对象
 * 3)、对外提供访问属性的静态方法
 * @author Administrator
 *
 */
class MyJvm2 {
    private static MyJvm2 instance =new MyJvm2();
    private MyJvm2(){
        
    }
    public static MyJvm2 getInstance (){        
        return instance;
    }

}
/**
 * 类在使用的时候加载 ,延缓加载时间
 * @author Administrator
 *
 */
class MyJvm3 {
    private static class JVMholder{
        private static MyJvm3 instance =new MyJvm3();
    }
    private MyJvm3(){
        
    }
    public static MyJvm3 getInstance (){        
        return JVMholder.instance;
    }

}

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值