JavaTM 2 Platform
Standard Ed. 5.0

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°¡

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°¡

InterruptedExceptionÀÌ Throw µÇ¾î ÇöÀçÀÇ threadÀÇ ÀÎÅÍ·´Æ® ½ºÅ×ÀÌÅͽº°¡ Ŭ¸®¾î µË´Ï´Ù.

ÁöÁ¤µÈ ´ë±â ½Ã°£ÀÌ °æ°úÇϸé, TimeoutExceptionÀ» ¹ß»ý½Ãŵ´Ï´Ù. ½Ã°£ÀÌ Á¦·Î ¶Ç´Â ±×°Íº¸´Ù ÀÛÀº °æ¿ì, ¸Þ¼­µå´Â ´ë±âÇÏÁö ¾Ê½À´Ï´Ù.

ÆĶó¹ÌÅÍ:
x - ±³È¯ÇÏ´Â °´Ã¼
timeout - ´ë±âÇÏ´Â ÃÖÀå ½Ã°£
unit - timeout ÀμöÀÇ ½Ã°£ ´ÜÀ§
¹Ýȯ°ª:
´Ù¸¥ thread¿¡ ÀÇÇØ Á¦°øµÇ´Â °´Ã¼
¿¹¿Ü:
InterruptedException - ´ë±âÁß¿¡ ÇöÀçÀÇ thread·Î ÀÎÅÍ·´Æ®°¡ ¹ß»ýÇßÀ» °æ¿ì
TimeoutException - º°ÀÇ thread°¡ ±³È¯¿¡ µé¾î°¡±â Àü¿¡ ÁöÁ¤µÈ ´ë±â ½Ã°£ÀÌ °æ°úÇßÀ» °æ¿ì

JavaTM 2 Platform
Standard Ed. 5.0

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