volatile static String name = "zhangsan";
static ThreadLocal<String> tl = new ThreadLocal<>();
public static void main(String[] args) {
tl.set("lusifer");
System.out.println(Thread.currentThread().getName()+"-- tl.get >"+tl.get());
new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName()+"-- tl.get >"+tl.get());
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"-- t1 > name "+name);
System.out.println(Thread.currentThread().getName()+"-- tl.get >"+tl.get());
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName()+"-- t2 > name "+name);
System.out.println(Thread.currentThread().getName()+"-- tl.get >"+tl.get());
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
name = "lisi";
tl.set("wangwu");
System.out.println(Thread.currentThread().getName()+"-- t2> name "+name);
}
}).start();
System.out.println(Thread.currentThread().getName()+"--main > name "+name);
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"-- tl.geteeeee >"+tl.get());
System.out.println(Thread.currentThread().getName()+"--main >两秒后的 name "+name);
}
public class UserInfo {
public static ThreadLocal<String> info = new ThreadLocal<String>();
public static String getInfo() {
return info.get();
}
public static void setInfo(String value) {
info.set(value);
}
}
public class NumUtil {
static ThreadLocal<Integer> addNum = ThreadLocal.withInitial(() -> 0);
public static int add10(int num) {
addNum.set(num);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return addNum.get() + 10;
}
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(20);
for (int i = 0; i < 20; i++) {
int num = i;
service.execute(() -> {
System.out.println(num + " " + NumUtil.add10(num));
});
}
service.shutdown();
}
}
public class DateUtil {
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static Date parse(String dateStr) {
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(200);
for (int i = 0; i < 200; i++) {
service.execute(() -> {
System.out.println(DateUtil.parse("2019-06-01 16:34:30"));
});
}
service.shutdown();
}
}