架构师养成记--6.单例和多线程、ThreadLocal

时间:2023-03-08 17:20:21
架构师养成记--6.单例和多线程、ThreadLocal

一、ThreadLocal

使用wait/notify方式实现的线程安全,性能将受到很大影响。解决方案是用空间换时间,不用锁也能实现线程安全。

来看一个小例子,在线程内的set、get就是threadLocal

 public class ConnThreadLocal {

     public static ThreadLocal<String> th = new ThreadLocal<String>();

     public void setTh(String value){
th.set(value);
}
public void getTh(){
System.out.println(Thread.currentThread().getName() + ":" + this.th.get());
} public static void main(String[] args) throws InterruptedException { final ConnThreadLocal ct = new ConnThreadLocal();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
ct.setTh("张三");
ct.getTh();
}
}, "t1"); Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
ct.getTh();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "t2"); t1.start();
t2.start();
} }

二、单例和多线程

单例常见的有饥饿模式和懒汉模式,但是这两个模式在多线程情况下是不行的。在多线程中考虑到新能和线程安全的问题一般考虑double check instance 和 static inner class这两种方式实现单例模式。

static inner class方式:

 public class InnerSingleton{
private static class Singleton{
private static Singleton single = new Singleton();
}
public Singleton getInstance(){
return Singleton.single;
}
}

double check instance 方式:

 public class DubbleSingleton {

     private static DubbleSingleton ds;

     public  static DubbleSingleton getDs(){
if(ds == null){
try {
//模拟初始化对象的准备时间...
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (DubbleSingleton.class) {
if(ds == null){
ds = new DubbleSingleton();
}
}
}
return ds;
} public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(DubbleSingleton.getDs().hashCode());
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(DubbleSingleton.getDs().hashCode());
}
},"t2");
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(DubbleSingleton.getDs().hashCode());
}
},"t3"); t1.start();
t2.start();
t3.start();
} }