使用同步解决懒汉式的线程安全问题

文章展示了在Java中,未使用同步机制时,多线程环境下可能导致单例模式失效,输出的Account对象地址不同。而引入同步后,确保了线程安全,两个线程获取到的是同一单例对象。
摘要由CSDN通过智能技术生成

未使用同步解决线程安全问题,并不是单例的,输出的两对象地址不相同

public class SingleTon {
    public static void main(String[] args) {
        CallableImpl callable = new CallableImpl();
        CallableImpl callable1 = new CallableImpl();
        FutureTask<Account> task = new FutureTask<Account>(callable);
        FutureTask<Account> task1 = new FutureTask<Account>(callable1);
        Thread thread = new Thread(task,"线程一");
        Thread thread1 = new Thread(task1,"线程二");
        thread.start();
        thread1.start();
        try {
            Account account = task.get();
            System.out.println("线程一返回结果:"+account);
            Account account1 = task1.get();
            System.out.println("线程二返回结果:"+account1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
class Account{
    private Account(){

    }
    private static Account instance = null;
    public static Account getInstance(){
//        if(instance == null){
//            synchronized (Account.class){
                if(instance == null){
                    instance = new Account();
                }
//            }
//        }
        return instance;
    }
}
class CallableImpl implements Callable<Account> {

    @Override
    public Account call() throws Exception {
        return Account.getInstance();
    }
}

这是输出结果:

使用同步解决线程安全问题后,是单例的,输出的两对象地址相同

public class SingleTon {
    public static void main(String[] args) {
        CallableImpl callable = new CallableImpl();
        CallableImpl callable1 = new CallableImpl();
        FutureTask<Account> task = new FutureTask<Account>(callable);
        FutureTask<Account> task1 = new FutureTask<Account>(callable1);
        Thread thread = new Thread(task,"线程一");
        Thread thread1 = new Thread(task1,"线程二");
        thread.start();
        thread1.start();
        try {
            Account account = task.get();
            System.out.println("线程一返回结果:"+account);
            Account account1 = task1.get();
            System.out.println("线程二返回结果:"+account1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
class Account{
    private Account(){

    }
    private static Account instance = null;
    public static Account getInstance(){
        if(instance == null){
            synchronized (Account.class){
                if(instance == null){
                    instance = new Account();
                }
            }
        }
        return instance;
    }
}
class CallableImpl implements Callable<Account> {

    @Override
    public Account call() throws Exception {
        return Account.getInstance();
    }
}

输出结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值