JavaTM 2 Platform
Standard Ed. 5.0

java.lang
Ŭ·¡½º ThreadLocal<T>

java.lang.Object 
  »óÀ§¸¦ È®Àå java.lang.ThreadLocal<T>
¾Ë·ÁÁø Á÷°è ¼­ºê Ŭ·¡½º:
InheritableThreadLocal

public class ThreadLocal<T>
extends Object

ÀÌ Å¬·¡½º´Â thread ·ÎÄà º¯¼ö¸¦ Á¦°øÇÕ´Ï´Ù. ÀÌ·¯ÇÑ º¯¼ö´Â get ¸Þ¼­µå ¶Ç´Â set ¸Þ¼­µå¸¦ »ç¿ëÇØ ¾×¼¼½º ÇÏ´Â thread°¡ °¢°¢ µ¶ÀÚÀûÀ¸·Î º¯¼öÀÇ ÃʱâÈ­µÈ º¹»çº»À» °®´Â´Ù°í ÇÏ´Â Á¡À¸·Î½á, º¸Åë ÀÇ º¯¼ö¿Í ´Ù¸¨´Ï´Ù. º¸Åë, ThreadLocal ÀνºÅϽº´Â »óŸ¦ thread¿¡ °ü·ÃÁöÀ¸·Á°í Çϴ Ŭ·¡½º¿¡¼­ÀÇ private static ÇʵåÀÔ´Ï´Ù (»ç¿ëÀÚ ID, Æ®·£Àè¼Ç(transaction) ID µî).

¿¹¸¦ µé¾î¾Æ·¡ÀÇ Å¬·¡½º¿¡¼­´Â private static ThreadLocal ÀνºÅϽº (serialNum)´Â Ŭ·¡½ºÀÇ static SerialNum.get() ¸Þ¼­µå¸¦ È£ÃâÇÏ´Â thread ¸¶´Ù ¡¸½Ã¸®¾ó ¹øÈ£¡¹¸¦ °ü¸®ÇÕ´Ï´Ù. threadÀÇ ½Ã¸®¾ó ¹øÈ£´Â SerialNum.get() ÃÖÃÊÀÇ È£Ãâ½Ã¿¡ ÇÒ´çÇÒ ¼ö ÀÖ¾î ±× ÈÄÀÇ È£Ãâ·Î º¯°æµÉ °ÍÀº ¾ø½À´Ï´Ù.


 public class SerialNum {
     // The next serial number to be assigned
     private static int nextSerialNum = 0;

     private static ThreadLocal serialNum = new ThreadLocal() {
         protected synchronized Object initialValue() {
             return new Integer(nextSerialNum++);
         }
     };

     public static int get() {
         return ((Integer) (serialNum.get())). intValue();
     }
 }
 

°¢ thread´Â thread°¡ »ýÁ¸ÇÏ°í ÀÖ¾î ThreadLocal ÀνºÅϽº°¡ ¾×¼¼½º °¡´ÉÇÑ »çÀÌ´Â thread ·ÎÄà º¯¼öÀÇ Ä«ÇÇ¿¡ÀÇ ¾Ï¹¬ÀûÀÎ ÂüÁ¶¸¦ º¸°ü À¯ÁöÇÕ´Ï´Ù. thread°¡ Á¾·áÇϸé, thread ·ÎÄà ÀνºÅϽºÀÇ Ä«ÇÇ´Â ¸ðµÎ °¡ºñÁö Ä÷ºÆ®µË´Ï´Ù (ÀÌ·¯ÇÑ Ä«ÇÇ¿¡ÀÇ ÂüÁ¶°¡ ±× ¹Û¿¡ Á¸ÀçÇÏ´Â °æ¿ì¸¦ Á¦¿ÜÇÏ´Ù).

µµÀÔµÈ ¹öÀü :
1.2

»ý¼ºÀÚ °³¿ä
ThreadLocal ()
          thread ·ÎÄà º¯¼ö¸¦ ÀÛ¼ºÇÕ´Ï´Ù.
 
¸Þ¼­µå °³¿ä
 T get ()
          ÀÌ thread ·ÎÄà º¯¼öÀÇ ÇöÇà threadÀÇ Ä«Çdz»ÀÇ °ªÀ» ¸®ÅÏÇÕ´Ï´Ù.
protected  T initialValue ()
          ÀÌ thread ·ÎÄà º¯¼ö¿¡ ´ëÇÑ ÇöÀçÀÇ threadÀÇ ÃʱⰪÀ» ¸®ÅÏÇÕ´Ï´Ù.
 void remove ()
          ÀÌ ThreadLocal °ªÀ» »èÁ¦ÇÕ´Ï´Ù.
 void set (T  value)
          ÀÌ thread ·ÎÄà º¯¼öÀÇ ÇöÀçÀÇ threadÀÇ Ä«ÇÇ°¡ ÁöÁ¤µÈ °ªÀ¸·Î ¼³Á¤ÇÕ´Ï´Ù.
 
Ŭ·¡½º java.lang. Object ·ÎºÎÅÍ »ó¼ÓµÈ ¸Þ¼­µå
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

»ý¼ºÀÚ »ó¼¼

ThreadLocal

public ThreadLocal()
thread ·ÎÄà º¯¼ö¸¦ ÀÛ¼ºÇÕ´Ï´Ù.

¸Þ¼­µåÀÇ »ó¼¼

initialValue

protected T  initialValue()
ÀÌ thread ·ÎÄà º¯¼ö¿¡ ´ëÇÑ ÇöÀçÀÇ threadÀÇ ÃʱⰪÀ» ¸®ÅÏÇÕ´Ï´Ù. ÀÌ ¸Þ¼­µå´Â thread ·ÎÄà ¸¶´Ù ¾×¼¼½º ÇÏ°í ÀÖ´Â thread ´ç 1ȸ ¸¸ È£ÃâÇÕ´Ï´Ù. ÃÖÃÊ, thread´Â get() ¸Þ¼­µå·Î º¯¼ö¿¡ ¾×¼¼½º ÇÕ´Ï´Ù. thread°¡ get ¸Þ¼­µå¸¦ È£ÃâÇϱâ Àü¿¡ set(T) ¸Þ¼­µå¸¦ È£ÃâÇÏ´Â °æ¿ì, initialValue ¸Þ¼­µå´Â thread·Î ºÒ·Á°¡ Áö ¾Ê½À´Ï´Ù.

ÀÌ ±¸ÇöÀº ´ÜÁö nullÀ» µ¹·ÁÁÙ »ÓÀÔ´Ï´Ù. ÇÁ·Î±×·¡¸Ó°¡ thread ·ÎÄà º¯¼ö¸¦ null ÀÌ¿ÜÀÇ °ªÀ¸·Î ÃʱâÈ­ÇÏ´Â °æ¿ì, ThreadLocal¸¦ »ó¼ÓÇØ, ÀÌ ¸Þ¼­µå¸¦ ¿À¹ö¶óÀ̵å(override) ÇÒ ÇÊ¿ä°¡ ÀÖ½À´Ï´Ù. º¸Åë, À͸íÀÇ ³»ºÎ Ŭ·¡½º°¡ »ç¿ëµË´Ï´Ù. initialValue ÀüÇüÀûÀÎ ±¸ÇöÀº ÀûÀýÇÑ »ý¼ºÀÚ¸¦ È£ÃâÇØ, »õ·Ó°Ô ±¸ÃàµÈ °´Ã¼¸¦ ¸®ÅÏÇÕ´Ï´Ù.

¹Ýȯ°ª:
ÀÌ thread ·ÎÄÃÀÇ ÃʱⰪ

get

public T  get()
ÀÌ thread ·ÎÄà º¯¼öÀÇ ÇöÇà threadÀÇ Ä«Çdz»ÀÇ °ªÀ» ¸®ÅÏÇÕ´Ï´Ù. thread·Î óÀ½À¸·Î ÀÌ ¸Þ¼­µå°¡ ºÒ·Á °¬À» ¶§´Â Ä«ÇÇ°¡ ÀÛ¼ºµÇ¾î ÃʱâÈ­µË´Ï´Ù.

¹Ýȯ°ª:
ÀÌ thread ·ÎÄÃÀÇ ÇöÇà threadÀÇ °ª

set

public void set(T  value)
ÀÌ thread ·ÎÄà º¯¼öÀÇ ÇöÀçÀÇ threadÀÇ Ä«ÇÇ°¡ ÁöÁ¤µÈ °ªÀ¸·Î ¼³Á¤ÇÕ´Ï´Ù. thread ·ÎÄÃÀÇ °ªÀ» ¼³Á¤Çϴµ¥ initialValue() ¸Þ¼­µå¿¡ Å©°Ô Á¸ÇÏ°í ÀÖ´Â ¸¹Àº ¾îÇø®ÄÉÀ̼ǿ¡¼­´Â ÀÌ ±â´ÉÀº ÇÊ¿ä ¾ø½À´Ï´Ù.

ÆĶó¹ÌÅÍ:
value - ÀÌ thread ·ÎÄÃÀÇ ÇöÇà threadÀÇ Ä«ÇÇ¿¡ Æ÷ÇԵǴ °ª

remove

public void remove()
ÀÌ ThreadLocal °ªÀ» »èÁ¦ÇÕ´Ï´Ù. »èÁ¦Çϸé, ThreadLocal Æ÷ÇÔ ¿ä°ÇÀÌ Àû°Ô µË´Ï´Ù. ÀÌ ThreadLocal°¡ Çѹø ´õ ¾×¼¼½º µÇ¸é, ThreadLocal´Â µðÆúÆ®·Î initialValue·Î ¼³Á¤µË´Ï´Ù.

µµÀÔµÈ ¹öÀü :
1.5

JavaTM 2 Platform
Standard Ed. 5.0

Copyright 2004 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms . Documentation Redistribution Policy µµ ÂüÁ¶ÇϽʽÿÀ.