ThreadLocal使用-隔离线程之间的值,不同线程可存取不同的值

ThreadLocal使用

  1. 实现线程之间变量隔离,不同线程存取不同的值。使用方法 例1
  2. 默认情况下初始值为null 可以通过继承类重写initialValue方法进行修改初始值

例1:


import java.util.Date;

class ThreadLocalExt extends ThreadLocal {
    @Override
    protected Object initialValue() {
        return "设置初始化值";
    }
}

class Tools {
    public static ThreadLocalExt threadLocalExt = new ThreadLocalExt();
}

class ThreadA08 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            if (Tools.threadLocalExt.get() == null) {
                Tools.threadLocalExt.set(new Date());
            }
            System.out.println(ThreadA.currentThread().getName() + "---" + Tools.threadLocalExt.get());

            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

public class StudyThreads08ThreadLocal的使用 {
    public static void main(String[] args) {
        ThreadA08 threadA08 = new ThreadA08();
        threadA08.start();
        for (int i = 0; i < 10; i++) {
            if (Tools.threadLocalExt.get() == null) {
                Tools.threadLocalExt.set(new Date());
            }
            Tools.threadLocalExt.set(new Date());
            System.out.println(ThreadA.currentThread().getName() + "---" + Tools.threadLocalExt.get());
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

InheritableThreadLocal 可以针对子线程和父线程返回不同的值

package com.chapter03;


import java.util.Date;

//class ThreadLocalExt extends ThreadLocal {
//    @Override
//    protected Object initialValue() {
//        return "设置初始化值";
//    }
//}

class Tools {
    public static ThreadLocalExt threadLocalExt = new ThreadLocalExt();
}

class ThreadA08 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            if (Tools.threadLocalExt.get() == null) {
                Tools.threadLocalExt.set(new Date());
            }
            System.out.println(ThreadA.currentThread().getName() + "---" + Tools.threadLocalExt.get());

            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

public class StudyThreads08ThreadLocal的使用 {
    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {
            if (Tools.threadLocalExt.get() == null) {
                Tools.threadLocalExt.set(new Date());
            }
            System.out.println(ThreadA.currentThread().getName() + "---" + Tools.threadLocalExt.get());
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


        ThreadA08 threadA08 = new ThreadA08();
        threadA08.start();
    }
}

class ThreadLocalExt extends InheritableThreadLocal {
    @Override
    protected Object initialValue() {
        return new Date();
    }

    @Override
    protected Object childValue(Object parentValue) {
        return parentValue + "针对子线程添加不同的内容";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值