반응형
java의 ThreadLocal을 이용하면 해당 Thread에서만 공유되는 전역변수를 이용할 수 있습니다.
아래 코드 처럼 static으로 선언하고 get, set을 이용해 값을 넣고 빼고 하면 됩니다.
static 이지만 Thread 별로 값이 관리 됩니다.
SpringSeucirty에서 Principal 공유나 TransactionManager 에서 사용된다고 합니다.
ThreadLocal Test Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; public class ThreadLocalTest { private static final ThreadLocal<Integer> threadLocal = new ThreadLocal<>(); private static final AtomicInteger threadCount = new AtomicInteger(); private static Integer threadGlobal = new Integer(0); public static void main(String[] args) throws InterruptedException { test(); } public static void test() throws InterruptedException { ExecutorService executor = Executors.newCachedThreadPool(); for (int i = 0; i < 100; i++) { executor.execute(new Runnable() { @Override public void run() { int i = threadGlobal = threadCount.incrementAndGet(); threadLocal.set(i); try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(String.format("Thread No : %d, Thread Loacl : %d, Thread Global : %d", i, threadLocal.get(), threadGlobal)); } }); } System.out.println("End"); } } |
Result
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | End Thread No : 38, Thread Loacl : 38, Thread Global : 72 Thread No : 91, Thread Loacl : 91, Thread Global : 91 Thread No : 52, Thread Loacl : 52, Thread Global : 85 Thread No : 83, Thread Loacl : 83, Thread Global : 85 Thread No : 10, Thread Loacl : 10, Thread Global : 20 Thread No : 90, Thread Loacl : 90, Thread Global : 91 Thread No : 45, Thread Loacl : 45, Thread Global : 71 Thread No : 13, Thread Loacl : 13, Thread Global : 32 Thread No : 85, Thread Loacl : 85, Thread Global : 90 Thread No : 87, Thread Loacl : 87, Thread Global : 90 Thread No : 1, Thread Loacl : 1, Thread Global : 18 Thread No : 81, Thread Loacl : 81, Thread Global : 85 Thread No : 35, Thread Loacl : 35, Thread Global : 90 Thread No : 93, Thread Loacl : 93, Thread Global : 100 Thread No : 21, Thread Loacl : 21, Thread Global : 31 Thread No : 40, Thread Loacl : 40, Thread Global : 72 Thread No : 66, Thread Loacl : 66, Thread Global : 85 Thread No : 33, Thread Loacl : 33, Thread Global : 62 Thread No : 78, Thread Loacl : 78, Thread Global : 85 Thread No : 6, Thread Loacl : 6, Thread Global : 18 Thread No : 56, Thread Loacl : 56, Thread Global : 85 Thread No : 79, Thread Loacl : 79, Thread Global : 85 Thread No : 39, Thread Loacl : 39, Thread Global : 72 Thread No : 100, Thread Loacl : 100, Thread Global : 100 Thread No : 97, Thread Loacl : 97, Thread Global : 100 Thread No : 65, Thread Loacl : 65, Thread Global : 85 Thread No : 42, Thread Loacl : 42, Thread Global : 72 Thread No : 53, Thread Loacl : 53, Thread Global : 85 Thread No : 23, Thread Loacl : 23, Thread Global : 33 Thread No : 59, Thread Loacl : 59, Thread Global : 85 Thread No : 54, Thread Loacl : 54, Thread Global : 85 Thread No : 95, Thread Loacl : 95, Thread Global : 100 Thread No : 27, Thread Loacl : 27, Thread Global : 71 Thread No : 96, Thread Loacl : 96, Thread Global : 100 Thread No : 2, Thread Loacl : 2, Thread Global : 18 Thread No : 26, Thread Loacl : 26, Thread Global : 33 Thread No : 73, Thread Loacl : 73, Thread Global : 85 Thread No : 36, Thread Loacl : 36, Thread Global : 90 Thread No : 62, Thread Loacl : 62, Thread Global : 85 Thread No : 74, Thread Loacl : 74, Thread Global : 85 Thread No : 60, Thread Loacl : 60, Thread Global : 85 Thread No : 5, Thread Loacl : 5, Thread Global : 18 Thread No : 92, Thread Loacl : 92, Thread Global : 95 Thread No : 32, Thread Loacl : 32, Thread Global : 62 Thread No : 49, Thread Loacl : 49, Thread Global : 70 Thread No : 43, Thread Loacl : 43, Thread Global : 72 Thread No : 64, Thread Loacl : 64, Thread Global : 85 Thread No : 46, Thread Loacl : 46, Thread Global : 71 Thread No : 63, Thread Loacl : 63, Thread Global : 85 Thread No : 61, Thread Loacl : 61, Thread Global : 85 Thread No : 22, Thread Loacl : 22, Thread Global : 31 Thread No : 44, Thread Loacl : 44, Thread Global : 72 Thread No : 11, Thread Loacl : 11, Thread Global : 32 Thread No : 28, Thread Loacl : 28, Thread Global : 71 Thread No : 75, Thread Loacl : 75, Thread Global : 85 Thread No : 17, Thread Loacl : 17, Thread Global : 32 Thread No : 31, Thread Loacl : 31, Thread Global : 70 Thread No : 51, Thread Loacl : 51, Thread Global : 85 Thread No : 18, Thread Loacl : 18, Thread Global : 34 Thread No : 48, Thread Loacl : 48, Thread Global : 70 Thread No : 69, Thread Loacl : 69, Thread Global : 85 Thread No : 89, Thread Loacl : 89, Thread Global : 91 Thread No : 82, Thread Loacl : 82, Thread Global : 85 Thread No : 30, Thread Loacl : 30, Thread Global : 71 Thread No : 55, Thread Loacl : 55, Thread Global : 85 Thread No : 70, Thread Loacl : 70, Thread Global : 85 Thread No : 24, Thread Loacl : 24, Thread Global : 33 Thread No : 84, Thread Loacl : 84, Thread Global : 90 Thread No : 41, Thread Loacl : 41, Thread Global : 72 Thread No : 20, Thread Loacl : 20, Thread Global : 34 Thread No : 8, Thread Loacl : 8, Thread Global : 17 Thread No : 4, Thread Loacl : 4, Thread Global : 17 Thread No : 88, Thread Loacl : 88, Thread Global : 90 Thread No : 7, Thread Loacl : 7, Thread Global : 18 Thread No : 94, Thread Loacl : 94, Thread Global : 100 Thread No : 3, Thread Loacl : 3, Thread Global : 18 Thread No : 47, Thread Loacl : 47, Thread Global : 72 Thread No : 57, Thread Loacl : 57, Thread Global : 85 Thread No : 76, Thread Loacl : 76, Thread Global : 85 Thread No : 14, Thread Loacl : 14, Thread Global : 32 Thread No : 15, Thread Loacl : 15, Thread Global : 32 Thread No : 12, Thread Loacl : 12, Thread Global : 32 Thread No : 86, Thread Loacl : 86, Thread Global : 90 Thread No : 68, Thread Loacl : 68, Thread Global : 85 Thread No : 9, Thread Loacl : 9, Thread Global : 18 Thread No : 77, Thread Loacl : 77, Thread Global : 85 Thread No : 25, Thread Loacl : 25, Thread Global : 33 Thread No : 58, Thread Loacl : 58, Thread Global : 85 Thread No : 98, Thread Loacl : 98, Thread Global : 100 Thread No : 50, Thread Loacl : 50, Thread Global : 85 Thread No : 34, Thread Loacl : 34, Thread Global : 52 Thread No : 16, Thread Loacl : 16, Thread Global : 32 Thread No : 19, Thread Loacl : 19, Thread Global : 34 Thread No : 99, Thread Loacl : 99, Thread Global : 100 Thread No : 67, Thread Loacl : 67, Thread Global : 85 Thread No : 29, Thread Loacl : 29, Thread Global : 71 Thread No : 37, Thread Loacl : 37, Thread Global : 72 Thread No : 72, Thread Loacl : 72, Thread Global : 85 Thread No : 80, Thread Loacl : 80, Thread Global : 85 Thread No : 71, Thread Loacl : 71, Thread Global : 85 |
반응형
'java' 카테고리의 다른 글
kisa sha256 java 예제 (0) | 2018.11.05 |
---|---|
mybatis jndi example (0) | 2018.11.05 |
mybatis jdbcTypeForNull NULL (0) | 2018.11.05 |
java, springboot, hikari example (0) | 2018.10.30 |
java - google otp (0) | 2018.10.19 |
spring boot logging.config (0) | 2018.09.10 |
restTemplate large file download stream (0) | 2018.09.03 |
java - pretty log - 예쁜 로그를 남기자! (0) | 2018.08.30 |