|
JavaTM 2 Platform Standard Ed. 5.0 |
|||||||||
ÀÌÀü Ŭ·¡½º ´ÙÀ½ Ŭ·¡½º | ÇÁ·¹ÀÓÀ¸·Î ÇÁ·¹ÀÓ ¾øÀÌ | |||||||||
°³¿ä: NESTED | Çʵå | constructor | ¸Þ¼µå | »ó¼¼: Çʵå | »ý¼ºÀÚ | ¸Þ¼µå |
java.lang.Objectjava.lang.ThreadLocal<T>
public class ThreadLocal<T>
ÀÌ Å¬·¡½º´Â 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 ·ÎÄà ÀνºÅϽºÀÇ Ä«ÇÇ´Â ¸ðµÎ °¡ºñÁö Ä÷ºÆ®µË´Ï´Ù (ÀÌ·¯ÇÑ Ä«ÇÇ¿¡ÀÇ ÂüÁ¶°¡ ±× ¹Û¿¡ Á¸ÀçÇÏ´Â °æ¿ì¸¦ Á¦¿ÜÇÏ´Ù).
»ý¼ºÀÚ °³¿ä | |
---|---|
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 |
»ý¼ºÀÚ »ó¼¼ |
---|
public ThreadLocal()
¸Þ¼µåÀÇ »ó¼¼ |
---|
protected T initialValue()
get()
¸Þ¼µå·Î º¯¼ö¿¡ ¾×¼¼½º ÇÕ´Ï´Ù. thread°¡ get ¸Þ¼µå¸¦ È£ÃâÇϱâ Àü¿¡ set(T)
¸Þ¼µå¸¦ È£ÃâÇÏ´Â °æ¿ì, initialValue ¸Þ¼µå´Â thread·Î ºÒ·Á°¡
Áö ¾Ê½À´Ï´Ù.
ÀÌ ±¸ÇöÀº ´ÜÁö nullÀ» µ¹·ÁÁÙ »ÓÀÔ´Ï´Ù. ÇÁ·Î±×·¡¸Ó°¡ thread ·ÎÄà º¯¼ö¸¦ null ÀÌ¿ÜÀÇ °ªÀ¸·Î ÃʱâÈÇÏ´Â °æ¿ì, ThreadLocal¸¦ »ó¼ÓÇØ, ÀÌ ¸Þ¼µå¸¦ ¿À¹ö¶óÀ̵å(override) ÇÒ Çʿ䰡 ÀÖ½À´Ï´Ù. º¸Åë, À͸íÀÇ ³»ºÎ Ŭ·¡½º°¡ »ç¿ëµË´Ï´Ù. initialValue ÀüÇüÀûÀÎ ±¸ÇöÀº ÀûÀýÇÑ »ý¼ºÀÚ¸¦ È£ÃâÇØ, »õ·Ó°Ô ±¸ÃàµÈ °´Ã¼¸¦ ¸®ÅÏÇÕ´Ï´Ù.
public T get()
public void set(T value)
initialValue()
¸Þ¼µå¿¡ Å©°Ô
Á¸Çϰí ÀÖ´Â ¸¹Àº ¾îÇø®ÄÉÀ̼ǿ¡¼´Â ÀÌ ±â´ÉÀº ÇÊ¿ä ¾ø½À´Ï´Ù.
value
- ÀÌ thread ·ÎÄÃÀÇ ÇöÇà threadÀÇ Ä«ÇÇ¿¡ Æ÷ÇԵǴ °ªpublic void remove()
|
JavaTM 2 Platform Standard Ed. 5.0 |
|||||||||
ÀÌÀü Ŭ·¡½º ´ÙÀ½ Ŭ·¡½º | ÇÁ·¹ÀÓÀ¸·Î ÇÁ·¹ÀÓ ¾øÀÌ | |||||||||
°³¿ä: NESTED | Çʵå | constructor | ¸Þ¼µå | »ó¼¼: Çʵå | »ý¼ºÀÚ | ¸Þ¼µå |
Copyright 2004 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms . Documentation Redistribution Policy µµ ÂüÁ¶ÇϽʽÿÀ.