Skip to content

Latest commit

ย 

History

History
159 lines (84 loc) ยท 4.93 KB

Thread_Local.md

File metadata and controls

159 lines (84 loc) ยท 4.93 KB

Thread Local


Thread Local ์™œ ์“ธ๊นŒ?

์ผ๋‹จ ์•„๋ž˜์˜ 3๊ฐœ์˜ ์ด๋ฏธ์ง€๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ณด์ž.




thread-A ์˜ ํ˜ธ์ถœ์ด ๋๋‚˜๋ฉด์„œ nameStore ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜๋ฐ›๋Š”๋ฐ, ์ด๋•Œ nameStore ๋Š” ์•ž์˜ 2๋ฒˆ์—์„œ userB ์˜ ๊ฐ’์œผ๋กœ ๋Œ€์ฒด๋˜์—ˆ๋‹ค.

๊ฒฐ๊ณผ์ ์œผ๋กœ Thread-A ์ž…์žฅ์—์„œ๋Š” ์ €์žฅํ•œ ๋ฐ์ดํ„ฐ์™€ ์กฐํšŒํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์ด์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ ์“ฐ๋ ˆ๋“œ๊ฐ€ ๋™์‹œ์— ๊ฐ™์€ ์ธ์Šคํ„ด์Šค์˜ ํ•„๋“œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฉด์„œ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ๋™์‹œ์„ฑ ๋ฌธ์ œ๋ผ ํ•œ๋‹ค.

์ด๋Ÿฐ ๋™์‹œ์„ฑ ๋ฌธ์ œ๋Š” ์—ฌ๋Ÿฌ ์“ฐ๋ ˆ๋“œ๊ฐ€ ๊ฐ™์€ ์ธ์Šคํ„ด์Šค์˜ ํ•„๋“œ์— ์ ‘๊ทผํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํŠธ๋ž˜ํ”ฝ์ด ์ ์€ ์ƒํ™ฉ์—์„œ๋Š” ํ™•๋ฅ ์ƒ ์ž˜ ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๊ณ , ํŠธ๋ž˜ํ”ฝ์ด ์ ์  ๋งŽ์•„์งˆ ์ˆ˜ ๋ก ์ž์ฃผ ๋ฐœ์ƒํ•œ๋‹ค.


โœ… ์ฐธ๊ณ 

์ด๋Ÿฐ ๋™์‹œ์„ฑ ๋ฌธ์ œ๋Š” ์ง€์—ญ ๋ณ€์ˆ˜์—์„œ๋Š” ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ง€์—ญ ๋ณ€์ˆ˜๋Š” ์“ฐ๋ ˆ๋“œ๋งˆ๋‹ค ๊ฐ๊ฐ ๋‹ค๋ฅธ ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์ด ํ• ๋‹น๋œ๋‹ค.

๋™์‹œ์„ฑ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ณณ์€ ๊ฐ™์€ ์ธ์Šคํ„ด์Šค์˜ ํ•„๋“œ(์ฃผ๋กœ ์‹ฑ๊ธ€ํ†ค์—์„œ ์ž์ฃผ ๋ฐœ์ƒ), ๋˜๋Š” static ๊ฐ™์€ ๊ณต์šฉ ํ•„๋“œ์— ์ ‘๊ทผํ•  ๋•Œ ๋ฐœ์ƒํ•œ๋‹ค. ๋™์‹œ์„ฑ ๋ฌธ์ œ๋Š” ๊ฐ’์„ ์ฝ๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค. ์–ด๋””์„ ๊ฐ€ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•œ๋‹ค.



Thread Local

์—ฌ๋Ÿฌ๊ฐœ์˜ ์Šค๋ ˆ๋“œ๊ฐ€ ์กด์žฌํ•  ๋•Œ, ํ•ด๋‹น ์Šค๋ ˆ๋“œ๋งŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ํŠน๋ณ„ํ•œ ์ €์žฅ์†Œ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๋” ํ’€์–ด์„œ ์ด์•ผ๊ธฐ ํ•ด๋ณด์ž. ๋งŒ์•ฝ ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ์Šค๋ ˆ๋“œ๊ฐ€ ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•œ๋‹ค๊ณ  ํ•  ๋•Œ, ์ด ๋ณ€์ˆ˜๋Š” ๊ณต์œ  ์ž์›์œผ๋กœ ๋™์‹œ์„ฑ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋‹ค๋ถ„ํ•˜๋‹ค. ThreadLocal ํด๋ž˜์Šค๋Š” ์˜ค์ง ํ•œ ์Šค๋ ˆ๋“œ์— ์˜ํ•ด ์ฝ๊ณ /์“ฐ์—ฌ์งˆ ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.


๊ฐ€๋ น Thread A ๊ฐ€ ThreadLocal ๋ณ€์ˆ˜( tLocal ) ์— ์ ‘๊ทผํ•˜์—ฌ a ๋ผ๋Š” ๋ฌธ์ž๋ฅผ ์ €์žฅํ•œ๋‹ค๊ณ  ํ•˜์ž. ๋™์‹œ์— Thread B ์—์„œ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ tLocal ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•˜์—ฌ, b ๋ผ๋Š” ๋ฌธ์ž๊ฐ’์„ ์ €์žฅํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ? a ๋ผ๋Š” ๋ฌธ์ž๋ฅผ b ๊ฐ€ ๋ฎ์–ด ๋ฒ„๋ฆฌ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์•„๋ž˜ ์ฒ˜๋Ÿผ ๊ฐ ์Šค๋ ˆ๋“œ๋ณ„ ์ „์šฉ ์ €์žฅ์†Œ์— ๊ฐ๊ฐ์˜ ๊ฐ’์ด ์ €์žฅ๋œ๋‹ค. ์ฆ‰ ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ๊ณต์œ  ์ž์›์˜ ๋™์‹œ์„ฑ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.




Thread Local Life Cycle



Thread Local Create

ThreadLocal<String> threadLocal = new ThreadLocal<>();

์ œ๋„ค๋ฆญ์„ ์‚ฌ์šฉํ•˜์—ฌ ํƒ€์ž… ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.



Thread Local write/read

threadLocal.set("a") // thread local ๊ฐ’ ์ €์žฅ
threadLocal.get()    // ์ €์žฅ๋œ ๊ฐ’ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

์„ ์–ธ๋œ threadLocal ๋ณ€์ˆ˜์˜ set(), get() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด Thread Local ๊ฐ’์„ ์“ฐ๊ณ  ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค.

์•„๋ฌด ๊ฐ’๋„ ์ €์žฅ๋˜์ง€ ์•Š์€ Thread Local์˜ ๊ฐ’์„ ์ฝ์œผ๋ฉด(get()) null ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.


@Slf4j
public class ThreadLocalServiceTest {

    private ThreadLocal<String> threadLocal = new ThreadLocal<>();
    
    @Test
    void field() {

        Runnable userC = () -> {
            log.info("start");

            String value = threadLocal.get();
            log.info("value : {}", value);

            log.info("set threadLocal value : A");
            threadLocal.set("A");

            value = threadLocal.get();
            log.info("threadLocal : {}", value);
        };

        Thread threadC = new Thread(userC);
        threadC.setName("thread-C");
        threadC.start();

    }
}



Thread Local remove

Thread Local์„ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜๊ณ  ๋‚˜๋ฉด ๋ฐ˜๋“œ์‹œ ThreadLocal.remove() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ, Thread Local์— ์ €์žฅ๋œ ๊ฐ’์„ ์ œ๊ฑฐํ•ด์•ผ ํ•œ๋‹ค.

Thread Local์˜ ๊ฐ’์„ ์ œ๊ฑฐํ•˜์ง€ ์•Š์œผ๋ฉด ์–ด๋–ค ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”์ง€ ์•„๋ž˜์˜ ์ด๋ฏธ์ง€๋ฅผ ํ†ตํ•ด ํ™•์ธํ•ด๋ณด์ž.



  1. ์‚ฌ์šฉ์ž A ๊ฐ€ ์ €์žฅ HTTP ์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค.

  2. WAS ๋Š” ์Šค๋ ˆ๋“œํ’€์—์„œ ์Šค๋ ˆ๋“œ๋ฅผ ํ•˜๋‚˜ ์กฐํšŒํ•œ๋‹ค.

  3. ํ•ด๋‹น ์Šค๋ ˆ๋“œ๋Š” thread-A ๊ฐ€ ํ• ๋‹น ๋˜์—ˆ๊ณ , ์‚ฌ์šฉ์ž A ์˜ ๋ฐ์ดํ„ฐ๋ฅผ Thread Local์— ์ €์žฅํ•œ๋‹ค.

  4. Thread Local thread-A ์ „์šฉ ๋ณด๊ด€์†Œ์— ์‚ฌ์šฉ์žA ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๊ด€ํ•œ๋‹ค.

  5. ์‚ฌ์šฉ์ž A ์˜ HTTP ์š”์ฒญ์ด ์ข…๋ฃŒ๋˜๋ฉด, thread-A ๋Š” ๋‹ค์‹œ ์Šค๋ ˆ๋“œ ํ’€์— ๋ฐ˜ํ™˜๋œ๋‹ค.

  6. thread-A ๋Š” ์Šค๋ ˆ๋“œ ํ’€์„ ํ†ตํ•ด์„œ ์žฌ์‚ฌ์šฉ ๋  ์ˆ˜ ์žˆ๋‹ค.



  1. ์‚ฌ์šฉ์ž B ์—์„œ HTTP ์กฐํšŒ ์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค.

  2. ์Šค๋ ˆ๋“œ ํ’€์—์„œ ํ•˜ํ•„ thread-A ๊ฐ€ ํ• ๋‹น๋˜์—ˆ๋‹ค.

  3. ์กฐํšŒ ์š”์ฒญ์ด๊ธฐ ๋•Œ๋ฌธ์— ์Šค๋ ˆ๋“œ ํ’€์˜ thread-A ์ „์šฉ ๊ณต๊ฐ„์—์„œ, ์ด์ „์— ์ €์žฅํ•ด๋‘” ๊ฐ’์ด ์กฐํšŒ๋œ๋‹ค.

  4. ์ฆ‰ ์‚ฌ์šฉ์ž B ๊ฐ€ ์˜ˆ์ƒํ•œ ์กฐํšŒ ๊ฒฐ๊ณผ๊ฐ€ ์•„๋‹Œ, ์‚ฌ์šฉ์ž A ๊ฐ€ ์ €์žฅํ•ด๋‘” ๊ฐ’์ด ์กฐํšŒ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.