单例模式是否线程安全

今天领导问了个问题说单例模式中,对象的变量是否是线程安全的。起初都不太确定,后来本着装的目的,试了一下,结果是否定的。在多线程环境下,对象的成员变量是不安全的。

package com.zhuyang.test;

public class Singleton {
private static final Singleton singleton;
//variable will be updated in muti-thread
private int count;
static {
singleton = new Singleton();
}

public static synchronized Singleton getInstance() {
return singleton;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

}



上面的代码是一段标准的单例模式。 其中有个成员变量count.他的值,会再多线程环境下不断修改。
package com.zhuyang.test;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ThreadTest t1=new ThreadTest(1111);
ThreadTest t2=new ThreadTest(2222);
ThreadTest t3=new ThreadTest(3333);
ThreadTest t4=new ThreadTest(4444);
ThreadTest t5=new ThreadTest(5555);
ThreadTest t6=new ThreadTest(6666);
ThreadTest t7=new ThreadTest(7777);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
t7.start();
}

}

class ThreadTest extends Thread {
private int threadID;

public ThreadTest(int threadID) {
this.threadID = threadID;
}
@Override
public void run() {
Singleton singleton = Singleton.getInstance();

singleton.setCount(threadID);
System.out.println(this.getName()+"is updating count value is="+this.getThreadID()+", count value of singleton is="+singleton.getCount());

}
public int getThreadID() {
return threadID;
}

public void setThreadID(int threadID) {
this.threadID = threadID;
}

}

运行上面的代码结果:
Thread-0is updating count value is=1111, count value of singleton is=4444
Thread-4is updating count value is=5555, count value of singleton is=4444
Thread-5is updating count value is=6666, count value of singleton is=4444
Thread-2is updating count value is=3333, count value of singleton is=4444
Thread-6is updating count value is=7777, count value of singleton is=4444
Thread-3is updating count value is=4444, count value of singleton is=4444
Thread-1is updating count value is=2222, count value of singleton is=4444

说明,多线程中,单例模式并非线程安全,即便是加上了synchronized关键字。

那么如何做到线程安全呢?做法就是把对象给synchronized掉,看代码。
package com.zhuyang.test;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ThreadTest t1=new ThreadTest(1111);
ThreadTest t2=new ThreadTest(2222);
ThreadTest t3=new ThreadTest(3333);
ThreadTest t4=new ThreadTest(4444);
ThreadTest t5=new ThreadTest(5555);
ThreadTest t6=new ThreadTest(6666);
ThreadTest t7=new ThreadTest(7777);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
t7.start();
}

}

class ThreadTest extends Thread {
private int threadID;

public ThreadTest(int threadID) {
this.threadID = threadID;
}
@Override
public void run() {
Singleton singleton = Singleton.getInstance();
synchronized (singleton) {
singleton.setCount(threadID);
System.out.println(this.getName()+"is updating count value is="+this.getThreadID()+", count value of singleton is="+singleton.getCount());
}
}

public int getThreadID() {
return threadID;
}

public void setThreadID(int threadID) {
this.threadID = threadID;
}

}


结果:

Thread-0is updating count value is=1111, count value of singleton is=1111
Thread-1is updating count value is=2222, count value of singleton is=2222
Thread-2is updating count value is=3333, count value of singleton is=3333
Thread-3is updating count value is=4444, count value of singleton is=4444
Thread-4is updating count value is=5555, count value of singleton is=5555
Thread-6is updating count value is=7777, count value of singleton is=7777
Thread-5is updating count value is=6666, count value of singleton is=6666
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值