java.util.concurrent
Ŭ·¡½º Exchanger<V>
java.lang.Object
java.util.concurrent.Exchanger<V>
- ÇüÅ ÆÄ¶ó¹ÌÅÍ:
V
- ±³È¯ °¡´ÉÇÑ °´Ã¼ÀÇ ÇüÅÂ
public class Exchanger<V>
- extends Object
2°³ÀÇ thread°¡ °´Ã¼¸¦ ±³È¯ °¡´ÉÇÑ µ¿±â Æ÷ÀÎÆ®. °¢ thread´Â ¿£Æ®¸®»óÀÇ °´Ã¼¸¦ exchange
¸Þ¼µå¿¡ Á¦°øÇØ, ´Ù¸¥ threadÀÇ Á¦°øÇÏ´Â °´Ã¼¸¦ ´ë½Å¿¡ ¹Þ½À´Ï´Ù.
»ç¿ë¿¹: ÀÌÇÏ¿¡
Exchanger¸¦ »ç¿ëÇØ thread°£¿¡ ¹öÆÛ¸¦ ±³È¯Çϴ Ŭ·¡½ºÀÇ ÁÖ¿ä ºÎºÐÀ» ³ªÅ¸³À´Ï´Ù. ¹öÆÛ¸¦ ä¿ì´Â thread°¡ Çʿ信 µû¶ó¼ »õ·Î¿î ºñ¾îÀÖ´Â ¹öÆÛ¸¦ ÃëµæÇØ, ¹öÆÛ¸¦ ºñ¿ì´Â thread¿¡ ä¿öÁø ¹öÆÛ¸¦ °Ç³×ÁÝ´Ï´Ù.
class FillAndEmpty {
Exchanger<DataBuffer> exchanger = new Exchanger();
DataBuffer initialEmptyBuffer = ... a made-up type
DataBuffer initialFullBuffer = ...
class FillingLoop implements Runnable {
public void run() {
DataBuffer currentBuffer = initialEmptyBuffer;
try {
while (currentBuffer ! = null) {
addToBuffer(currentBuffer);
if (currentBuffer.full())
currentBuffer = exchanger.exchange(currentBuffer);
}
} catch (InterruptedException ex) { ... handle ... }
}
}
class EmptyingLoop implements Runnable {
public void run() {
DataBuffer currentBuffer = initialFullBuffer;
try {
while (currentBuffer ! = null) {
takeFromBuffer(currentBuffer);
if (currentBuffer.empty())
currentBuffer = exchanger.exchange(currentBuffer);
}
} catch (InterruptedException ex) { ... handle ...}
}
}
void start() {
new Thread(new FillingLoop()). start();
new Thread(new EmptyingLoop()). start();
}
}
- µµÀÔµÈ ¹öÀü
:
- 1.5
»ý¼ºÀÚ °³¿ä |
Exchanger ()
»õ·Î¿î Exchanger¸¦ ÀÛ¼ºÇÕ´Ï´Ù. |
¸Þ¼µå °³¿ä |
V |
exchange (V x)
ÀÎÅÍ·´Æ® °¡ ¹ß»ýÇÏÁö ¾Ê´Â ÇÑ, ´Ù¸¥ thread°¡ ÀÌ ±³È¯ Æ÷ÀÎÆ®¿¡ µµ´ÞÇÒ ¶§±îÁö ´ë±âÇØ, ÁöÁ¤µÈ °´Ã¼¸¦ ±× thread¿¡ Àü¼Û ÇØ, ´ë½Å¿¡ °´Ã¼¸¦ ¹Þ½À´Ï´Ù. |
V |
exchange (V x,
long timeout,
TimeUnit unit)
ÀÎÅÍ·´Æ® °¡ ¹ß»ýÇÏ´ÂÁö, ÁöÁ¤µÈ ´ë±â ½Ã°£ÀÌ °æ°úÇÏÁö ¾Ê´Â ÇÑ, ´Ù¸¥ thread°¡ ÀÌ ±³È¯ Æ÷ÀÎÆ®¿¡ µµ´ÞÇÒ ¶§±îÁö ´ë±âÇØ, ÁöÁ¤µÈ °´Ã¼¸¦ ±× thread¿¡ Àü¼Û ÇØ, ´ë½Å¿¡ °´Ã¼¸¦ ¹Þ½À´Ï´Ù. |
Ŭ·¡½º java.lang. Object ·ÎºÎÅÍ »ó¼ÓµÈ ¸Þ¼µå |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Exchanger
public Exchanger()
- »õ·Î¿î Exchanger¸¦ ÀÛ¼ºÇÕ´Ï´Ù.
exchange
public V exchange(V x)
throws InterruptedException
ÀÎÅÍ·´Æ®
°¡ ¹ß»ýÇÏÁö ¾Ê´Â ÇÑ, ´Ù¸¥ thread°¡ ÀÌ ±³È¯ Æ÷ÀÎÆ®¿¡ µµ´ÞÇÒ ¶§±îÁö ´ë±âÇØ, ÁöÁ¤µÈ °´Ã¼¸¦ ±× thread¿¡ Àü¼Û ÇØ, ´ë½Å¿¡ °´Ã¼¸¦ ¹Þ½À´Ï´Ù.
´Ù¸¥ thread°¡ À̹Ì
±³È¯ Æ÷ÀÎÆ®·Î ´ë±âÇϰí ÀÖ´Â °æ¿ì, Àç°³ÇØ threadÀÇ ½ºÄÉÁÙ¸µÀ» ½ÇÇàÇØ, ÇöÀçÀÇ thread°¡ °Ç³×ÁÖ´Â °´Ã¼¸¦ ¹Þ½À´Ï´Ù. ÇöÀçÀÇ thread´Â Áï½Ã º¹±ÍÇØ, ´Ù¸¥ thread·ÎºÎÅÍ ±³È¯¿ëÀ¸·Î °Ç³×¹Þ´Â °´Ã¼¸¦ ¹Þ½À´Ï´Ù.
±³È¯ Æ÷ÀÎÆ®·Î ´ë±âÁßÀÇ ´Ù¸¥ thread°¡ Á¸ÀçÇÏÁö ¾Ê´Â °æ¿ì, ÇöÀçÀÇ thread´Â thread ½ºÄÉÁÙ¸µ¿¡ ´ëÇØ ¹«È¿·Î ¿©°ÜÁ® ´ÙÀ½ÀÇ 2°³
Áß ÇÑÂÊÀÌ ¹ß»ýÇÒ ¶§±îÁö ´ë±âÇÕ´Ï´Ù.
- ´Ù¸¥ thread°¡ ±³È¯¿¡ µé¾î°£´Ù
- ´Ù¸¥ thread°¡ ÇöÀçÀÇ thread¿¡
ÀÎÅÍ·´Æ®
¸¦ ½Ç½ÃÇÑ´Ù
ÇöÀçÀÇ thread°¡
- ÀÌ ¸Þ¼µå¿¡ÀÇ ¿£Æ®¸®»ó¿¡¼ ¼³Á¤µÈ ÀÎÅÍ·´Æ® ½ºÅ×ÀÌÅͽº¸¦ º¸°ü À¯ÁöÇÏ´ÂÁö,
- ±³È¯ÀÇ ´ë±âÁß¿¡
ÀÎÅÍ·´Æ®
°¡ ¹ß»ýÇßÀ» °æ¿ì,
InterruptedException
ÀÌ Throw µÇ¾î ÇöÀçÀÇ threadÀÇ ÀÎÅÍ·´Æ® ½ºÅ×ÀÌÅͽº°¡ Ŭ¸®¾î µË´Ï´Ù.
- ÆÄ¶ó¹ÌÅÍ:
x
- ±³È¯ÇÏ´Â °´Ã¼
- ¹Ýȯ°ª:
- ´Ù¸¥ thread¿¡ ÀÇÇØ Á¦°øµÇ´Â °´Ã¼
- ¿¹¿Ü:
InterruptedException
- ´ë±âÁß¿¡
ÇöÀçÀÇ thread·Î ÀÎÅÍ·´Æ®°¡ ¹ß»ýÇßÀ» °æ¿ì
exchange
public V exchange(V x,
long timeout,
TimeUnit unit)
throws InterruptedException,
TimeoutException
ÀÎÅÍ·´Æ®
°¡ ¹ß»ýÇÏ´ÂÁö, ÁöÁ¤µÈ ´ë±â ½Ã°£ÀÌ °æ°úÇÏÁö ¾Ê´Â ÇÑ, ´Ù¸¥ thread°¡ ÀÌ ±³È¯ Æ÷ÀÎÆ®¿¡ µµ´ÞÇÒ ¶§±îÁö ´ë±âÇØ, ÁöÁ¤µÈ °´Ã¼¸¦ ±× thread¿¡ Àü¼Û ÇØ, ´ë½Å¿¡ °´Ã¼¸¦ ¹Þ½À´Ï´Ù.
´Ù¸¥ thread°¡ À̹Ì
±³È¯ Æ÷ÀÎÆ®·Î ´ë±âÇϰí ÀÖ´Â °æ¿ì, Àç°³ÇØ threadÀÇ ½ºÄÉÁÙ¸µÀ» ½ÇÇàÇØ, ÇöÀçÀÇ thread°¡ °Ç³×ÁÖ´Â °´Ã¼¸¦ ¹Þ½À´Ï´Ù. ÇöÀçÀÇ thread´Â Áï½Ã º¹±ÍÇØ, ´Ù¸¥ thread·ÎºÎÅÍ ±³È¯¿ëÀ¸·Î °Ç³×¹Þ´Â °´Ã¼¸¦ ¹Þ½À´Ï´Ù.
±³È¯ Æ÷ÀÎÆ®·Î ´ë±âÁßÀÇ ´Ù¸¥ thread°¡ Á¸ÀçÇÏÁö ¾Ê´Â °æ¿ì, ÇöÀçÀÇ thread´Â thread ½ºÄÉÁÙ¸µ¿¡ ´ëÇØ ¹«È¿·Î ¿©°ÜÁ® ´ÙÀ½ÀÇ 3 °³Áß ÇÑÂÊÀÌ ¹ß»ýÇÒ ¶§±îÁö ´ë±âÇÕ´Ï´Ù.
- ´Ù¸¥ thread°¡ ±³È¯¿¡ µé¾î°£´Ù
- ´Ù¸¥ thread°¡ ÇöÀçÀÇ thread¿¡
ÀÎÅÍ·´Æ®
¸¦ ½Ç½ÃÇÑ´Ù - ÁöÁ¤µÈ ´ë±â ½Ã°£ÀÌ °æ°úÇÑ´Ù
ÇöÀçÀÇ thread°¡
- ÀÌ ¸Þ¼µå¿¡ÀÇ ¿£Æ®¸®»ó¿¡¼ ¼³Á¤µÈ ÀÎÅÍ·´Æ® ½ºÅ×ÀÌÅͽº¸¦ º¸°ü À¯ÁöÇÏ´ÂÁö,
- ±³È¯ÀÇ ´ë±âÁß¿¡
ÀÎÅÍ·´Æ®
°¡ ¹ß»ýÇßÀ» °æ¿ì,
InterruptedException
ÀÌ Throw µÇ¾î ÇöÀçÀÇ threadÀÇ ÀÎÅÍ·´Æ® ½ºÅ×ÀÌÅͽº°¡ Ŭ¸®¾î µË´Ï´Ù.
ÁöÁ¤µÈ ´ë±â ½Ã°£ÀÌ °æ°úÇϸé, TimeoutException
À» ¹ß»ý½Ãŵ´Ï´Ù. ½Ã°£ÀÌ Á¦·Î ¶Ç´Â ±×°Íº¸´Ù ÀÛÀº °æ¿ì, ¸Þ¼µå´Â ´ë±âÇÏÁö ¾Ê½À´Ï´Ù.
- ÆÄ¶ó¹ÌÅÍ:
x
- ±³È¯ÇÏ´Â °´Ã¼timeout
- ´ë±âÇÏ´Â ÃÖÀå ½Ã°£unit
- timeout ÀμöÀÇ ½Ã°£ ´ÜÀ§
- ¹Ýȯ°ª:
- ´Ù¸¥ thread¿¡ ÀÇÇØ Á¦°øµÇ´Â °´Ã¼
- ¿¹¿Ü:
InterruptedException
- ´ë±âÁß¿¡
ÇöÀçÀÇ thread·Î ÀÎÅÍ·´Æ®°¡ ¹ß»ýÇßÀ» °æ¿ì
TimeoutException
- º°ÀÇ thread°¡ ±³È¯¿¡ µé¾î°¡±â Àü¿¡
ÁöÁ¤µÈ ´ë±â ½Ã°£ÀÌ °æ°úÇßÀ» °æ¿ì
Copyright 2004 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms . Documentation Redistribution Policy µµ
ÂüÁ¶ÇϽʽÿÀ.