thread local

This commit is contained in:
xiongraorao 2018-08-06 09:58:14 +08:00
parent f2599feb9d
commit b25b457461
2 changed files with 76 additions and 0 deletions

View File

@ -0,0 +1,31 @@
package com.raorao;
/**
* .
*
* @author Xiong Raorao
* @since 2018-08-02-16:46
*/
public class Test {
private int a;
public static void main(String[] args) {
//System.out.println(foo());
int r = new Test().foo();
System.out.println(r);
}
public int foo() {
try {
System.out.println("sadfasdfasdf");
return a;
} catch (Exception e) {
e.printStackTrace();
return 3;
} finally {
System.out.println("finally: " + ++a);
return a;
}
}
}

View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2018. Xiong Raorao. All rights reserved.
* Project Name: book-notes
* File Name: Test.java
* Date: 18-7-25 下午5:32
* Author: Xiong Raorao
*/
package com.raorao.java.thread;
/**
* .
*
* @author Xiong Raorao
* @since 2018-07-25-17:32
*/
public class ThreadLocalTest {
private static ThreadLocal<Integer> local = new ThreadLocal<>();
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
System.out.println(" I am t1");
local.set(1);
try {
Thread.sleep(2000);
System.out.println("t1 value: " + local.get());
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread.sleep(2000);
Thread t2 = new Thread(() -> {
System.out.println("I am t2");
System.out.println("before set , I get " + local.get());
try {
Thread.sleep(2000);
local.set(2);
System.out.println("set after 2 s, I get " + local.get());
} catch (InterruptedException e) {
e.printStackTrace();
}
});
t2.start();
t1.start();// 无论怎么更换两个线程的启动顺序得到的值是不一样的
}
}