[Java] 纯文本查看 复制代码 private static ThreadLocal<SimpleDateFormat> threadLocal = ThreadLocal.withInitial(()->new SimpleDateFormat("yyyy"));
private static Object a,b,c;
public static void main(String[] args) throws ParseException, InterruptedException {
Thread threadA = new Thread(()->{
Object object = threadLocal.get();
a= object;
System.out.println("线程名:"+Thread.currentThread().getName()+",输出结果"+object);
});
threadA.setName("A");
Thread threadB = new Thread(()->{
Object object = threadLocal.get();
b = object;
System.out.println("线程名:"+Thread.currentThread().getName()+",输出结果"+object);
});
threadB.setName("B");
Thread threadC = new Thread(()->{
Object object = threadLocal.get();
c=object;
System.out.println("线程名:"+Thread.currentThread().getName()+",输出结果"+object);
});
threadC.setName("C");
threadA.start();
threadB.start();
threadC.start();
threadA.join();
threadB.join();
threadC.join();
System.out.println(b==c);
System.out.println(c==a);
System.out.println(a==b);
}
输出结果[Asm] 纯文本查看 复制代码 线程名:A,输出结果java.text.SimpleDateFormat@38d640
线程名:C,输出结果java.text.SimpleDateFormat@38d640
线程名:B,输出结果java.text.SimpleDateFormat@38d640
false
false
false
输出的结果调用toString,由于重写了SimpleDateFormat(问题中提到的Project对象同理)hashcode,看到 线程ABC输出结果java.text.SimpleDateFormat@38d640 都是一样的,
但是通过 把对象赋值 abc三个静态变量,然后发现 全是false,
[Java] 纯文本查看 复制代码 //SimpleDateFormat的hashCode
//实际为调用String的hashCode
public class SimpleDateFormat extends DateFormat {
private String pattern;
@Override
public int hashCode()
{
return pattern.hashCode();
// just enough fields for a reasonable distribution
}
}
|