【JAVA-线程】ThreadLocal

在这里插入图片描述
1创建ThreadLocal对象

private static ThreadLocal<Integer> threadLocal= new
			ThreadLocal<>();

2.使用initialValue()更改初始化的值

private static ThreadLocal<Integer> threadLocalinitial = new
		ThreadLocal<Integer>() {
	protected Integer initialValue() {
		return 200;
	};
};

3.java8后 使用withInitial(()->{ return 111;})

private static ThreadLocal<Integer> threadLocal8 =
ThreadLocal.withInitial(()->{
	return 8888;
});

4.使用set设置ThreadLocal对象的值

public static void main(String[] args) {
	threadLocal.set(10026);
	
	System.out.println(
			Thread.currentThread().getName()+
			"--->"+threadLocal.get());
	System.out.println("initialValue设置的值为:"+threadLocalinitial.get());
	System.out.println("withInitial设置的值为:"+threadLocal8.get());
}

在这里插入图片描述

代码:

public class ThreadLock01 {
	//1空ThreadLocal对象
	private static ThreadLocal<Integer> threadLocal= new
			ThreadLocal<>();
	//2使用InitialValue()更改初始化的值
	private static ThreadLocal<Integer> threadLocalinitial = new
			ThreadLocal<Integer>() {
		protected Integer initialValue() {
			return 200;
		};
	};
	//3JDK8之后 使用withInitial(()->{ return 111;})
	private static ThreadLocal<Integer> threadLocal8=
			ThreadLocal.withInitial(()->{
				return 8888;
			});
	
	
	public static void main(String[] args) {
		threadLocal.set(10026);
		
		System.out.println(
				Thread.currentThread().getName()+
				"--->"+threadLocal.get());
		System.out.println("initialValue设置的值为:"+threadLocalinitial.get());
		System.out.println("withInitial设置的值为:"+threadLocal8.get());
		
		new Thread(()->lam()).start();
		
		new Thread(new MyRun()).start();
	}
	
	private static void lam() {
		threadLocal.set((int)(Math.random()*99));
		System.out.println("lam  "+Thread.currentThread().getName()+threadLocal.get());
	}
	
	public static class MyRun implements Runnable{
		@Override
		public void run() {
			// TODO Auto-generated method stub
			threadLocal.set((int)(Math.random()*99));
			System.out.println("MyRun  "+Thread.currentThread().getName()+threadLocal.get());
		}
	}

}

在这里插入图片描述
二:
ThreadLocal分析上下文环境 起点
1 构造器:哪里调用就属于哪里 找线程体
2 run方法 本线程自身的

代码:

public class ThreadLocal03 {
	private static ThreadLocal<Integer> threadLocal=
			ThreadLocal.withInitial(()->1);
	
	
	public static void main(String[] args) {
		new Thread(new R()).start();
	}
	
	static class R implements Runnable{
		public R() {
			System.out.println(Thread.currentThread().getName()+
					"  "+threadLocal.get());
		}
		
		public void run() {
			// TODO Auto-generated method stub
			System.out.println(Thread.currentThread().getName()+
					"  最后  "+threadLocal.get());
		}
		
	}
}

在这里插入图片描述

InheritableThreadLocal

继承上下文环境 的数据,初始为空但是会根据上文的改变而改变
创建:

private static ThreadLocal<Integer> inheritableThreadLocal = 
			new InheritableThreadLocal<>();

代码:

public class ThreadLocal03 {
	private static ThreadLocal<Integer> threadLocal = 
			new ThreadLocal<>();
	private static ThreadLocal<Integer> inheritableThreadLocal = 
			new InheritableThreadLocal<>();
	public static void main(String[] args) {
		threadLocal.set(1);
		inheritableThreadLocal.set(1);
		System.out.println(Thread.currentThread().getName()+
				" threadLocal:"+threadLocal.get()+" \n  inheritableThreadLocal:"+inheritableThreadLocal.get());
		new Thread(()->{
			System.out.println(Thread.currentThread().getName()+
					" threadLocal:"+threadLocal.get()+" \n  inheritableThreadLocal:"+inheritableThreadLocal.get());
		},"t1").start();
		threadLocal.set(2);
		inheritableThreadLocal.set(2);
		new Thread(()->{
			System.out.println(Thread.currentThread().getName()+
					" threadLocal:"+threadLocal.get()+" \n  inheritableThreadLocal:"+inheritableThreadLocal.get());
		},"t2").start();
	}
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值