码迷,mamicode.com
首页 > 编程语言 > 详细

线程学习--(六)单例和多线程、ThreadLocal

时间:2017-06-14 11:41:15      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:实现   alt   解决   []   thread   span   pack   logs   static   

一、ThreadLocal

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

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

技术分享
package thread2;

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("abc");
                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();
    }
    
}
View Code

技术分享

 

二、单例和多线程

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

static inner class方式:

package bhz.base.conn011;

public class Singletion {
    
    private static class InnerSingletion {
        private static Singletion single = new Singletion();
    }
    
    public static Singletion getInstance(){
        return InnerSingletion.single;
    }
    
}

double check instance 方式:

技术分享
package thread2;

public class DubbleSingleton {

    private static DubbleSingleton ds;
    
    public  static DubbleSingleton getDs(){
        if(ds == null){
            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();
    }
    
}
View Code

 

线程学习--(六)单例和多线程、ThreadLocal

标签:实现   alt   解决   []   thread   span   pack   logs   static   

原文地址:http://www.cnblogs.com/code-java/p/6893207.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!