package com.test;
import java.util.HashMap;
import java.util.Map;
public class MyThreadLocal {
private Map<String, Thread> map = new HashMap<String, Thread>();
//获取当前对象
public Thread get(){
Thread thread = new Thread();
//存放当前线程的信息
if(map.get(thread) == null){
map.put(thread.toString(), thread);
return thread;
}else{
return map.get(thread);
}
}
//从map中移除当前线程
public void remove(){
Thread th = Thread.currentThread();
if(map.get(th) != null){
map.remove(th);
}
}
public static void main(String[] args) {
MyThreadLocal my = new MyThreadLocal();
Thread th = my.get();
System.out.println(th);
}
}
这里只是根据个人的理解,简单的模拟了一下其实现原理,日后再补充吧