Thread Local Java SE 1.2

Thread μ˜μ—­μ— λ³€μˆ˜λ₯Ό μ„€μ •ν•˜μ—¬ νŠΉμ • Thread κ°€ μ‹€ν–‰λ˜λŠ” λͺ¨λ“  μ½”λ“œμ—μ„œ ν•΄λ‹Ή Thread 에 μ„€μ •λœ λ³€μˆ˜κ°’μ„ μ‚¬μš©ν•  수 μžˆλ‹€.

Syntax

생성

ThreadLocal thread = new ThreadLocal();

μ‚¬μš©

thread.set("String");
thread.get();

μ œλ„€λ¦­

ThreadLocal thread = new ThreadLocal<String>();

thread.set("Generic Variable");
String message = (String) thread.get();

μ΄ˆκΈ°ν™”

ThreadLocal thread = new ThreadLocal<String>() {
  @Override
  protected String initialValue() {
    return "This is initial value";
  }
}

Usage

  • μ‚¬μš©μž 인증 데이터 μ „νŒŒ
  • Transection μ»¨ν…μŠ€νŠΈ μ „νŒŒ
  • Thread Safe ν•œ λ°μ΄ν„°μ˜ 보관

Warning

TheadPool ν™˜κ²½μ—μ„œ ThreadLocal 을 μ‚¬μš©ν•˜λŠ” 경우 ThreadLocal λ³€μˆ˜μ— λ³΄κ΄€λœ λ°μ΄ν„°μ˜ μ‚¬μš©μ΄ λλ‚˜λ©΄ λ°˜λ“œμ‹œ ν•΄λ‹Ή 데이터λ₯Ό μ‚­μ œν•΄μ£Όμ–΄μ•Ό ν•œλ‹€.

그렇지 μ•ŠλŠ” 경우 μž¬μ‚¬μš©λ˜λŠ” Thread κ°€ μ˜¬λ°”λ₯΄μ§€ μ•ŠλŠ” 데이터λ₯Ό μ°Έμ‘° ν•  수 μžˆλ‹€.