今天领导问了个问题说单例模式中,对象的变量是否是线程安全的。起初都不太确定,后来本着装的目的,试了一下,结果是否定的。在多线程环境下,对象的成员变量是不安全的。
上面的代码是一段标准的单例模式。 其中有个成员变量count.他的值,会再多线程环境下不断修改。
运行上面的代码结果:
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掉,看代码。
结果:
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
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