网站首页 美食营养 游戏数码 手工爱好 生活家居 健康养生 运动户外 职场理财 情感交际 母婴教育 生活知识 知识问答

java线程--ThreadLocal

时间:2024-10-13 13:21:26

1、package com.zhang.thread.threadlocal01;public class Run { public static ThreadLocal<String> t1 = new ThreadLocal<>(); public static void main(String[] args) { if(t1.get() == null){ System.out.println("t1从来没有放过值"); t1.set("hello"); } System.out.println(t1.get()); System.out.println(t1.get()); }}

java线程--ThreadLocal

2、get()为空时,说明ThreadLocal中从来没有放过值;当ThreadLocal中存在值时,可以使用get()从中取值,值依然保留

© 2026 一点知道
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com