码迷,mamicode.com
首页 > 其他好文 > 详细

Thread 与 ThreadLocal

时间:2019-11-21 16:52:15      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:stack   bbb   默认   tac   load   out   set   trace   ext   

@Test
public void testThread() {
Thread thread = Thread.currentThread();
System.out.println("thread:" + thread);
//当前线程 id
System.out.println("threadId:" + thread.getId());
//当前线程名称
System.out.println("threadName:" + thread.getName());
//当前线程状态
System.out.println("threadState:" + thread.getState());
//当前线程优先级
System.out.println("threadPriority:" + thread.getPriority());
//当前线程组名称
System.out.println("threadThreadGroup,Name:" + thread.getThreadGroup().getName());
//当前线程组父类
System.out.println("threadThreadGroup,parent:" + thread.getThreadGroup().getParent());
//当前线程组的最高优先级别
System.out.println("threadThreadGroup,MaxPriority:" + thread.getThreadGroup().getMaxPriority());
System.out.println("threadStackTrace:" + thread.getStackTrace());
System.out.println("ContextClassLoader:" + thread.getContextClassLoader());
}


@Test
public void testThreadLocal() {
ThreadLocal<Object> threadLocal = new ThreadLocal<>();
//默认为 null,初始值为 null
System.out.println(threadLocal.get());
//给当前线程设置值
threadLocal.set("aaa");
System.out.println(threadLocal.get());
threadLocal.set("bbb");
System.out.println(threadLocal.get());
//清空线程值
threadLocal.remove();
}

Thread 与 ThreadLocal

标签:stack   bbb   默认   tac   load   out   set   trace   ext   

原文地址:https://www.cnblogs.com/ming-blogs/p/11906506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!