博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
类ThreadLocal的使用
阅读量:4230 次
发布时间:2019-05-26

本文共 2196 字,大约阅读时间需要 7 分钟。

  变量值的共享可以使用public static变量的形式,所有的线程都使用同一个public static变量。如果想实现每一个线程

都有自己的共享变量。JDK中提供的类ThreadLocal正是为了解决这样的问题。

类ThreadLocal主要解决的就是每个线程绑定自己的值。

1. 方法get()与null

public class Run {    public static ThreadLocal t1=new ThreadLocal();    public static void main(String[] args){        if(t1.get()==null){            System.out.println("从未放过值");            t1.set("我的值");        }        System.out.println(t1.get());        System.out.println(t1.get());    }}
效果:

从未放过值

我的值
我的值

2.验证线程变量的隔离性

看例子

public class ThreadA extends Thread {    @Override    public void run() {        try {            for (int i = 0; i < 100; i++) {                Tools.t1.set("ThreadA" + (i + 1));                System.out.println("ThreadA get Value " + Tools.t1.get());                Thread.sleep(200);            }        } catch (InterruptedException e) {            e.printStackTrace();        }    }}
public class ThreadB extends Thread{    @Override    public void run() {        try{            for(int i=0;i<100;i++) {                Tools.t1.set("ThreadB" + (i + 1));                System.out.println("ThreadB get Value=" + Tools.t1.get());                Thread.sleep(200);            }        }catch (InterruptedException e){            e.printStackTrace();        }    }}
public class Run {    public static void main(String[] args1){        try{            ThreadA a=new ThreadA();            ThreadB b=new ThreadB();            a.start();            b.start();            for(int i=0;i<100;i++){                Tools.t1.set("Main"+(i+1));                System.out.println("Main get Value="+Tools.t1.get());                Thread.sleep(200);            }        }catch (InterruptedException e){            e.printStackTrace();        }    }}

3.解决get()返回null问题

通过给定初始化值

public class ThreadLocalExt extends ThreadLocal{    @Override    protected Object initialValue() {        return "我是默认值 第一次get不再为null";    }}
public class Run {    public static ThreadLocalExt t1=new ThreadLocalExt();    public static void main(String[] args){        if(t1.get()==null){            System.out.println("从未放过值");            t1.set("我的值");        }        System.out.println(t1.get());        System.out.println(t1.get());    }}

转载地址:http://dmjqi.baihongyu.com/

你可能感兴趣的文章
Red Hat Fedora 5 Unleashed
查看>>
AdvancED Flash Interface Design (Advanced Design)
查看>>
Quartz Job Scheduling Framework: Building Open Source Enterprise Applications
查看>>
C++ GUI Programming with Qt 4
查看>>
Effective Use of Microsoft Enterprise Library: Building Blocks for Creating Enterprise Applications
查看>>
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
查看>>
Moodle E-learning Course Development
查看>>
VoIP For Dummies
查看>>
Administrator's Guide to SQL Server 2005
查看>>
Ajax Design Patterns
查看>>
DNS and BIND (5th Edition)
查看>>
Firewall Fundamentals
查看>>
Learning PHP and MySQL
查看>>
Agile Software Construction
查看>>
Computer Security Basics
查看>>
Sams Teach Yourself MySQL in 10 Minutes
查看>>
Information Systems : The State of the Field
查看>>
IPv6 Essentials
查看>>
Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner
查看>>
Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
查看>>