Java SE 1.2
Thread LocalThread μμμ λ³μλ₯Ό μ€μ νμ¬ νΉμ 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
κ° μ¬λ°λ₯΄μ§ μλ λ°μ΄ν°λ₯Ό μ°Έμ‘° ν μ μλ€.
β Intersection Type Garbege Collector β