From ed3da08f024fe465114aac545e1afd0500a4320a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BA=9C=E5=BC=BA?= <8569561+fuqiangma@user.noreply.gitee.com> Date: Tue, 10 Jan 2023 02:12:16 +0000 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20threadlo?= =?UTF-8?q?caldemo/ThreadLocal=5FDemo/src/com/itheima/threadlocal/Demo01.j?= =?UTF-8?q?ava?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/itheima/threadlocal/Demo01.java | 48 ------------------- 1 file changed, 48 deletions(-) delete mode 100644 threadlocaldemo/ThreadLocal_Demo/src/com/itheima/threadlocal/Demo01.java diff --git a/threadlocaldemo/ThreadLocal_Demo/src/com/itheima/threadlocal/Demo01.java b/threadlocaldemo/ThreadLocal_Demo/src/com/itheima/threadlocal/Demo01.java deleted file mode 100644 index 5b36d05..0000000 --- a/threadlocaldemo/ThreadLocal_Demo/src/com/itheima/threadlocal/Demo01.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.itheima.threadlocal; - -import java.util.HashMap; - -/* - 同一线程中: 存的变量和取的变量要一致 - */ -public class Demo01 { - ThreadLocal tl = new ThreadLocal<>(); - - private String content; - - public String getContent() { -// return content; - //取的时候: 获取当前线程绑定的局部变量 - return tl.get(); - } - - public void setContent(String content) { -// this.content = content; - //存的时候 : content变量就跟当前线程绑定了 - tl.set(content); - } - - public static void main(String[] args) { - Demo01 demo01 = new Demo01(); - - //开启5个线程 - for (int i = 0; i < 5; i++) { - Thread t = new Thread(){ - @Override - public void run() { - /* - * 预期: 例如存的时候线程0 : 线程0的数据 - * 取出的时候, 线程0 对应的 线程0的数据 - * */ - demo01.setContent(Thread.currentThread().getName() + "的数据"); - System.out.println("-----------------------------------------"); - String content = demo01.getContent(); - System.out.println(Thread.currentThread().getName() + ":" + content); -// HashMap - } - }; - t.setName("线程" + i);// 线程0~4 - t.start(); - } - } -}