Search
Duplicate
๐Ÿ€

ThreadLocal

Data ์˜์—ญ์€ ํ”„๋กœ๊ทธ๋žจ์˜ย ์ „์—ญ ๋ณ€์ˆ˜์™€ย ์ •์ (Static)๋ณ€์ˆ˜
Heap ์˜์—ญ์€ ๊ฐ์ฒด ์ƒ์„ฑ์‹œ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น
Stack ์˜์—ญ์€ ์ง€์—ญ ๋ณ€์ˆ˜์™€ ๋งค๊ฐœ ๋ณ€์ˆ˜
์ด ๋•Œ ํ•˜๋‚˜์˜ ์“ฐ๋ ˆ๋“œ์—์„œ๋งŒ ๊ณต์œ ํ•ด์•ผ ํ•˜๋Š” ๋ณ€์ˆ˜๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ํ•˜๋‚˜์˜ ์“ฐ๋ ˆ๋“œ๊ฐ€ ํ• ๋‹น๋˜์–ด MVC ํ๋ฆ„์œผ๋กœ ์‘๋‹ต๊นŒ์ง€ ์ฑ…์ž„์ง„๋‹ค. ์ด ๋•Œ ํŠธ๋žœ์žญ์…˜๊ณผ ๊ฐ™์ด ํ•ด๋‹น ์š”์ฒญ์— ๋Œ€ํ•ด์„œ๋งŒ ๊ณต์œ ๋ฅผ ํ•ด์•ผ ํ•˜๋Š” ์ž์›์€ ํ•ด๋‹น ์“ฐ๋ ˆ๋“œ๋งŒ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋งŒ์•ฝ ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ heap ๋ฉ”๋ชจ๋ฆฌ์—์„œ ๊ด€๋ฆฌํ•œ๋‹ค๋ฉด ์ฒ˜์Œ ์š”์ฒญ ๋“ค์–ด์˜ฌ๋•Œ๋ถ€ํ„ฐ ์‘๋‹ต ๋‚˜๊ฐˆ๋•Œ๊นŒ์ง€ ํ•ด๋‹น ๋ณ€์ˆ˜ ๋ฉ”๋ชจ๋ฆฌ์˜ ์ฃผ์†Œ๋ฅผ ๊ณ„์† ์ธ์ž๋กœ ๊ฐ€์ง€๊ณ  ๊ฐ€์•ผ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๋งค์šฐ ๋น„ํšจ์œจ์ . ๊ทธ๋ž˜์„œ ThreadLocal ์ด ์ƒ๊ฒจ๋‚จ.
ThreadLocal: ํ•˜๋‚˜์˜ ์“ฐ๋ ˆ๋“œ์—์„œ ๊ณต์œ ํ•˜๋Š” ์ „์—ญ ๋ณ€์ˆ˜
๊ทธ๋ ‡๋‹ค๋ฉด ThreadLocal ์€ ์–ด๋””์— ์ €์žฅ๋ ๊นŒ?
heap memory ์— ์ €์žฅ๋œ๋‹ค. ThreadLocal ์ด ๊ทธ๋Œ€๋กœ ์ €์žฅ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ณ  ThreadLocalMap ์— ์ €์žฅ๋œ๋‹ค.
the values associated with a ThreadLocal variable are stored in a special data structure called a ThreadLocalMap, which is stored in the heap memory of the current thread.
ThreadLocalMap ์€ ํ˜„์žฌ ์“ฐ๋ ˆ๋“œ์˜ ํž™ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋œ๋‹ค.
ํž™ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋˜๊ธฐ๋•Œ๋ฌธ์— ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ ๋Œ€์ƒ์ด๋‹ค.