线程使用示例:ThreadLocal

ThreadLocal作用类似于线程中的局部变量,单可以在外部进行引用,赋值,其他线程访问不到这个对象引用,可以看出每个线程私有的变量。在一个给定线程中首次调用 get 时, 会调用 initialValue 方法。在此之后,get 方法会返回属于当前线程的那个实例。或者当线程运行中执行set后,同一个线程get后返回的是同一个对象。通过源代码可以得知使用了弱引用-当gc回收后对象也回收(Thread中有个map ThreadLocalMap 中的Entry是个对ThreadLocal对象的弱引用,map的键是ThreadLocal 值是传入的value),一般对象引用是强引用,还有软引用,gc后仍不消失,只要弱引用对象(WeakReference)对应的引用(weakObject)不消失可以通过weakObject.get()方法获得队列中的对象(存入弱引用的值),可以作为缓存来使用。虚引用-第一次get也获得不到,作为堆外对象的管理使用。

package com.example.test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class TestThreadLocal {
    public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static final ThreadLocal<SimpleDateFormat> simpleDateFormatThreadLocal =
            ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));

    private static final int threadCount = 15;
    private static Thread[] threads;

    public static void main(String[] args) {
        threads = new Thread[threadCount];

        for (int i = 0; i < threadCount; i++) {
            threads[i] = new Thread(() -> {

                String dateStr = dateFormat.format(new Date());
                System.out.println("dateFormat:" + dateStr);
                dateFormat.setTimeZone(TimeZone.getTimeZone("Etc/GMT+11"));
                String dateStr1 = simpleDateFormatThreadLocal.get().format(new Date());
                System.out.println("simpleDateFormatThreadLocal:" + dateStr1);
            });
            threads[i].start();
        }
    }

}

测试代码

package com.example.test;


class TestThreadLocal implements AutoCloseable{
    public static final ThreadLocal<Integer> integerThreadLocal=new ThreadLocal<>();

    public void setThreadLocal(Integer value) {
       integerThreadLocal.set(value);
    }

    public  Integer getThreadLocal() {
        return integerThreadLocal.get();
}

    @Override
    public void close() {
        integerThreadLocal.remove();
    }
}

public class MainClass{
    public static void main(String[] args) throws InterruptedException {
        try (TestThreadLocal testThreadLocal = new TestThreadLocal()) {
            System.out.println("主线程开始");
            System.out.println("主线程修改唯一的ThreadLocal对象的值为5");
            testThreadLocal.setThreadLocal(5);
            new Thread2().start();
            while (true) {
                System.out.println("main-如果互不影响将在main线程和Thread2线程看到不同的值 \n" +
                        testThreadLocal.getThreadLocal());
                Thread.sleep(2000);
            }

        }
    }
}

class Thread2 extends Thread {

    @Override
    public void run() {
        try (TestThreadLocal testThreadLocal=new TestThreadLocal()){
            System.out.println("Thread2 线程修改唯一的ThreadLocal对象的值为10");
            testThreadLocal.setThreadLocal(10);
            while (!isInterrupted()) {
                try {
                    System.out.println("thread2-如果互不影响将在main线程和Thread2线程看到不同的值\n" +
                            +testThreadLocal.getThreadLocal());
                    Thread.sleep(2000);
                }catch (InterruptedException E){
                    break;
                }finally {
                }
            }
        }
    }
}

主线程开始
主线程修改唯一的ThreadLocal对象的值为5
main-如果互不影响将在main线程和Thread2线程看到不同的值
5
Thread2 线程修改唯一的ThreadLocal对象的值为10
thread2-如果互不影响将在main线程和Thread2线程看到不同的值
10
main-如果互不影响将在main线程和Thread2线程看到不同的值
5
thread2-如果互不影响将在main线程和Thread2线程看到不同的值
10
main-如果互不影响将在main线程和Thread2线程看到不同的值
5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值