|
JavaTM 2 Platform Standard Ed. 5.0 |
|||||||||
ÀÌÀü Ŭ·¡½º ´ÙÀ½ Ŭ·¡½º | ÇÁ·¹ÀÓÀ¸·Î ÇÁ·¹ÀÓ ¾øÀÌ | |||||||||
°³¿ä: NESTED | Çʵå | constructor | ¸Þ¼µå | »ó¼¼: Çʵå | »ý¼ºÀÚ | ¸Þ¼µå |
java.lang.Objectjava.util.concurrent.CyclicBarrier
public class CyclicBarrier
thread ¼¼Æ®ÀÇ °¢°¢ÀÌ °øÅëÀÇ ¹Ù¸®¾î Æ÷ÀÎÆ®¿¡ À̸¦ ¶§±îÁö ´ë±âÇÏ´Â °ÍÀ» °¡´ÉÇÏ°Ô ÇÏ´Â µ¿±âÈ Áö¿ø ±â´É. CyclicBarrier´Â ¼·Î ´ë±âÇÏ´Â °ÍÀÌ ¶§¶§·Î ÇÊ¿äÇÏ°Ô µÇ´Â °íÁ¤ »çÀÌÁîÀÇ thread ÆÄƼ°¡ °ü°èµÈ ÇÁ·Î±×·¥À¸·Î À¯¿ëÇÕ´Ï´Ù. ¹Ù¸®¾î´Â ´ë±âÁßÀÇ thread°¡ ÇØ¹æµÈ ÈÄ¿¡ ÀçÀÌ¿ëÇÒ ¼ö Àֱ⠶§¹®¿¡ ¡¸¼øÈ¯½Ä¡¹À̶ó°í ºÒ¸³´Ï´Ù.
CyclicBarrier ´Â
¿É¼Ç Runnable
Ä¿¸àµå¸¦ Áö¿ø
ÇÕ´Ï´Ù. À̰ÍÀº ¸¶Áö¸· thread°¡ µµÂøÇÑ ÈÄ¿¡
¾î´À threadµµ ÇØ¹æµÇ±â Àü¿¡
¹Ù¸®¾î Æ÷ÀÎÆ® ¸¶´Ù 1ȸ
½ÇÇàµË´Ï´Ù. ÀÌ ¡¸¹Ù¸®¾î ¾×¼Ç¡¹Àº ÆÄƼÁß ÇÑÂÊÀÌ Ã³¸®¸¦ ¼ÓÇàÇϱâ Àü¿¡ °øÀ¯ »óŸ¦ °»½ÅÇϴµ¥ µµ¿òÀÌ µË´Ï´Ù.
»ç¿ë·Ê: Â÷¿¡ º´·Ä ºÐÇØ ¼³°è¿¡¼ÀÇ ¹Ù¸®¾îÀÇ »ç¿ë¿¹¸¦ ³ªÅ¸³À´Ï´Ù.
class Solver { final int N; final float[][] data; final CyclicBarrier barrier; class Worker implements Runnable { int myRow; Worker(int row) { myRow = row; } public void run() { while (! done()) { processRow(myRow); try { barrier.await(); } catch (InterruptedException ex) { return; } catch (BrokenBarrierException ex) { return; } } } } public Solver(float[][] matrix) { data = matrix; N = matrix.length; barrier = new CyclicBarrier(N, new Runnable() { public void run() { mergeRows(...); } }); for (int i = 0; i < N; ++i) new Thread(new Worker(i)). start(); waitUntilDone(); } }¿©±â¼, °¢ ¿öÅ©½º ·¹µå´Â Çà·ÄÀÇ ÇàÀ» ó¸®ÇØ, ¸ðµç ÇàÀÌ Ã³¸®µÉ ¶§±îÁö ¹Ù¸®¾î·Î ´ë±âÇÕ´Ï´Ù. ¸ðµç ÇàÀÌ Ã³¸®µÇ¸é, ÁöÁ¤µÈ
Runnable
¹Ù¸®¾î ¾×¼ÇÀÌ ½ÇÇàµÇ¾î, ÇàÀ» ¸ÓÁö ÇÕ´Ï´Ù. ¼Ö·ç¼ÇÀÌ °ËÃâµÈ °ÍÀ» ¸¶Àð°¡ ÆÇº°Çϸé, done()°¡ true¸¦ µ¹·ÁÁÖ°í, °¢ ¿öÄ«°¡ Á¾·áÇÕ´Ï´Ù.
¹Ù¸®¾î ¾×¼ÇÀÌ ½ÇÇà½Ã¿¡ Áß´ÜµÈ ÆÄƼ¿¡
Á¸Çϰí ÀÖÁö ¾Ê´Â °æ¿ì, ±× ¾×¼ÇÀÌ ÇØ¹æµÇ¸é, ÆÄƼ³»ÀÇ ¸î°³ÀÇ thread°¡ ±×°ÍÀ» ½ÇÇàÇÒ ¼ö ÀÖ½À´Ï´Ù. À̰ÍÀ» ¿ëÀÌÇÏ°Ô Çϱâ À§ÇÑ, await()
°¢ È£ÃâÀº ¹Ù¸®¾îÀÇ À§Ä¡¿¡¼ ±× threadÀÇ µµÂø À妽º¸¦ ¸®ÅÏÇÕ´Ï´Ù. ±× ÈÄ, ¹Ù¸®¾î ¾×¼ÇÀ» ½ÇÇàÇÏ´Â thread¸¦ ¼±ÅÃÇÒ ¼ö ÀÖ½À´Ï´Ù. ´ÙÀ½¿¡ ¿¹¸¦ ³ªÅ¸³À´Ï´Ù.
if (barrier.await() == 0) { // log the completion of this iteration }
CyclicBarrier´Â
½ÇÆÐÇÑ µ¿±âÈÀÇ ½Ãµµ¿¡ ´ëÇØ¼
ÆÄ½ºÆ®ÆäÀÏ·Î ÀüÀΰ¡ ¹«Àΰ¡ÀÇ Àý´Ü ¸ðµ¨À» »ç¿ëÇÕ´Ï´Ù. ³¢¾îµé¾î, ½ÇÆÐ, ¶Ç´Â ŸÀÓ ¾Æ¿ôÀ» À§Çؼ thread°¡ ¹Ù¸®¾î Æ÷ÀÎÆ®¸¦ ¹Ì¼º¼÷·ÎÀÇÇØ µÎ¸é, ÀüÀÇ await()
·ÎºÎÅÍ Àç°³µÇ¾î ÀÖÁö ¾ÊÀº thread¸¦ Æ÷ÇÔÇÑ, ´Ù¸¥ ¸ðµç thread¿¡¼µµ BrokenBarrierException
(À̰͵鿡¼µµ °ÅÀÇ µ¿½Ã¿¡ ÀÎÅÍ·´Æ®°¡ ¹ß»ýÇÏ´Â °æ¿ì´Â InterruptedException)¸¦ °³ÀÔ½ÃÄÑ ÀÌ»óÇÑ »óŰ¡ °è¼ÓµË´Ï´Ù.
CountDownLatch
»ý¼ºÀÚ °³¿ä | |
---|---|
CyclicBarrier (int parties)
ÁöÁ¤µÈ ¼öÀÇ ÆÄƼ (thread)°¡ ´ë±â »óÅ¿¡ ÀÖ´Â °æ¿ì¿¡ ¿©Çà ÇÏ´Â »õ·Î¿î CyclicBarrier¸¦ ÀÛ¼ºÇÕ´Ï´Ù. |
|
CyclicBarrier (int parties,
Runnable barrierAction)
ÁöÁ¤µÈ ¼öÀÇ ÆÄƼ (thread)°¡ ´ë±â »óÅ¿¡ ÀÖ´Â °æ¿ì¿¡ ¿©Çà ÇÏ´Â »õ·Î¿î CyclicBarrier¸¦ ÀÛ¼ºÇÕ´Ï´Ù. |
¸Þ¼µå °³¿ä | |
---|---|
int |
await ()
¸ðµç ÆÄƼ °¡ ÀÌ ¹Ù¸®¾î·Î await¸¦ È£ÃâÇÒ ¶§±îÁö ´ë±âÇÕ´Ï´Ù. |
int |
await (long timeout,
TimeUnit unit)
¸ðµç ÆÄƼ °¡ ÀÌ ¹Ù¸®¾î·Î await¸¦ È£ÃâÇÒ ¶§±îÁö ´ë±âÇÕ´Ï´Ù. |
int |
getNumberWaiting ()
¹Ù¸®¾î·Î ´ë±âÇϰí ÀÖ´Â ÆÄƼÀÇ ¼ö¸¦ ¸®ÅÏÇÕ´Ï´Ù. |
int |
getParties ()
ÀÌ ¹Ù¸®¾îÀÇ ¿©Çà¿¡ ÇÊ¿äÇÑ ÆÄƼÀÇ ¼ö¸¦ ¸®ÅÏÇÕ´Ï´Ù. |
boolean |
isBroken ()
ÀÌ ¹Ù¸®¾î°¡ °íÀå »óÅ¿¡ ÀÖÀ»Áö ¾î¶³Áö¸¦ ¹®ÀÇÇÕ´Ï´Ù. |
void |
reset ()
¹Ù¸®¾î¸¦ Ãʱ⠻óÅ¿¡ ¸®¼Â ÇÕ´Ï´Ù. |
Ŭ·¡½º java.lang. Object ·ÎºÎÅÍ »ó¼ÓµÈ ¸Þ¼µå |
---|
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
»ý¼ºÀÚ »ó¼¼ |
---|
public CyclicBarrier(int parties, Runnable barrierAction)
parties
- ¹Ù¸®¾î°¡ ¿©Çà Çϱâ Àü¿¡ await()
¸¦
È£ÃâÇÒ Çʿ䰡 ÀÖ´Â threadÀÇ ¼öbarrierAction
- ¹Ù¸®¾îÀÇ Æ®¸³½Ã¿¡ ½ÇÇàÇÏ´Â Ä¿¸àµå. ¾×¼ÇÀÌ Á¸ÀçÇÏÁö ¾Ê´Â °æ¿ì´Â null
IllegalArgumentException
- parties°¡ 1 ¹Ì¸¸ÀÇ °æ¿ìpublic CyclicBarrier(int parties)
parties
- ¹Ù¸®¾î°¡ ¿©Çà Çϱâ Àü¿¡ await()
¸¦
È£ÃâÇÒ Çʿ䰡 ÀÖ´Â threadÀÇ ¼ö
IllegalArgumentException
- parties°¡ 1 ¹Ì¸¸ÀÇ °æ¿ì¸Þ¼µåÀÇ »ó¼¼ |
---|
public int getParties()
public int await() throws InterruptedException, BrokenBarrierException
ÆÄƼ
°¡ ÀÌ ¹Ù¸®¾î·Î await¸¦ È£ÃâÇÒ ¶§±îÁö ´ë±âÇÕ´Ï´Ù.
ÇöÀçÀÇ thread°¡ ¸¶Áö¸·¿¡ µµÂøÇÏ´Â thread°¡ ¾Æ´Ñ °æ¿ì, threadÀÇ ½ºÄÉÁÙ¸µ¿¡ ´ëÇØ¼´Â ¹«È¿·Î ¿©°ÜÁ® ´ÙÀ½Áß ÇÑÂÊÀÌ ¹ß»ýÇÒ ¶§±îÁö ´ë±âÇÕ´Ï´Ù.
ÀÎÅÍ·´Æ®
¸¦ ½Ç½ÃÇÑ´Ù ÀÎÅÍ·´Æ®
¸¦ ½Ç½ÃÇÑ´Ùreset()
¸¦
È£ÃâÇÑ´Ù ÇöÀçÀÇ thread·Î
ÀÎÅÍ·´Æ®
°¡ ¹ß»ýÇßÀ» °æ¿ì,
InterruptedException
ÀÌ Throw µÇ¾î ÇöÀçÀÇ threadÀÇ ÀÎÅÍ·´Æ® ½ºÅ×ÀÌÅͽº°¡ Ŭ¸®¾î µË´Ï´Ù.
¸î°³ÀÇ thread°¡ ´ë±âÁß¿¡ ¹Ù¸®¾î·Î reset()
°¡ ½ÇÇàµÇ´ÂÁö, await
È£Ãâ½Ã ¶Ç´Â ¸î°³ÀÇ thread°¡ ´ë±âÁß¿¡ ¹Ù¸®¾î°¡ÆÄ±«µÇ¾úÀ»
°æ¿ì, BrokenBarrierException
À» ¹ß»ý½Ãŵ´Ï´Ù.
´ë±âÁß¿¡ ¸î°³ÀÇ thread·ÎÀÎÅÍ·´Æ®
°¡ ¹ß»ýÇßÀ» °æ¿ì, ´ë±âÁßÀÇ ´Ù¸¥ thread°¡ ¸ðµÎ BrokenBarrierException
¸¦
Throw ÇØ, ¹Ù¸®¾î°¡ °íÀå »óÅ¿¡ ³õ¿©Áý´Ï´Ù.
ÇöÀçÀÇ thread°¡ µµÂøÇÏ´Â ¸¶Áö¸· threadÀ̸ç, »ý¼ºÀÚ³»¿¡¼ nullÀÌ ¾Æ´Ñ ¹Ù¸®¾î ¾×¼ÇÀÌ ÁöÁ¤µÇ´Â °æ¿ì, ÇöÀçÀÇ thread´Â ¾×¼ÇÀ» ½ÇÇàÇÏ°í ³ª¼, ´Ù¸¥ threadÀÇ ¼ÓÇàÀ» Çã¿ëÇÕ´Ï´Ù. ¹Ù¸®¾î ¾×¼ÇÁß¿¡ ¿¹¿Ü°¡ ¹ß»ýÇϸé, ÇöÀçÀÇ thread³»¿¡¼ ±× ¿¹¿Ü°¡ ¼Û½ÅµÇ¾î ¹Ù¸®¾î°¡ °íÀå »óÅ¿¡ ³õ¿©Áý´Ï´Ù.
getParties()
- 1 Àº ÃÖÃÊ·Î µµÂøÇÏ´Â thread¸¦, Á¦·Î´Â ¸¶Áö¸·¿¡ µµÂøÇÏ´Â thread¸¦ ³ªÅ¸³½´Ù
InterruptedException
- ´ë±âÁß¿¡
ÇöÀçÀÇ thread·Î ÀÎÅÍ·´Æ®°¡ ¹ß»ýÇßÀ» °æ¿ì
BrokenBarrierException
- ÇöÀçÀÇ threadÀÇ ´ë±âÁß¿¡ ´Ù¸¥ thread·Î ÀÎÅÍ·´Æ®°¡ ¹ß»ýÇÑ, ¹Ù¸®¾î°¡ ¸®¼Â µÈ, await
È£Ãâ½Ã¿¡ ¹Ù¸®¾î°¡ ÆÄ±«µÈ, ¶Ç´Â ¿¹¿Ü¸¦ À§Çؼ ¹Ù¸®¾î ¾×¼Ç (Á¸ÀçÇÏ´Â °æ¿ì)ÀÌ ½ÇÆÐÇßÀ» °æ¿ìpublic int await(long timeout, TimeUnit unit) throws InterruptedException, BrokenBarrierException, TimeoutException
ÆÄƼ
°¡ ÀÌ ¹Ù¸®¾î·Î await¸¦ È£ÃâÇÒ ¶§±îÁö ´ë±âÇÕ´Ï´Ù.
ÇöÀçÀÇ thread°¡ ¸¶Áö¸·¿¡ µµÂøÇÏ´Â thread°¡ ¾Æ´Ñ °æ¿ì, threadÀÇ ½ºÄÉÁÙ¸µ¿¡ ´ëÇØ¼´Â ¹«È¿·Î ¿©°ÜÁ® ¾Æ·¡ÀÇ ¾î¶² °ÍÀΰ¡°¡ ¹ß»ýÇÒ ¶§±îÁö ´ë±âÇÕ´Ï´Ù.
ÀÎÅÍ·´Æ®
¸¦ ½Ç½ÃÇÑ´Ù ÀÎÅÍ·´Æ®
¸¦ ½Ç½ÃÇÑ´Ùreset()
¸¦
È£ÃâÇÑ´Ù ÇöÀçÀÇ thread·Î
ÀÎÅÍ·´Æ®
°¡ ¹ß»ýÇßÀ» °æ¿ì,
InterruptedException
ÀÌ Throw µÇ¾î ÇöÀçÀÇ threadÀÇ ÀÎÅÍ·´Æ® ½ºÅ×ÀÌÅͽº°¡ Ŭ¸®¾î µË´Ï´Ù.
ÁöÁ¤µÈ ´ë±â ½Ã°£ÀÌ °æ°úÇϸé, TimeoutException
À» ¹ß»ý½Ãŵ´Ï´Ù. ½Ã°£ÀÌ Á¦·Î ¶Ç´Â ±×°Íº¸´Ù ÀÛÀº °æ¿ì, ¸Þ¼µå´Â ´ë±âÇÏÁö ¾Ê½À´Ï´Ù.
¸î°³ÀÇ thread°¡ ´ë±âÁß¿¡ ¹Ù¸®¾î·Î reset()
°¡ ½ÇÇàµÇ´ÂÁö, await
È£Ãâ½Ã ¶Ç´Â ¸î°³ÀÇ thread°¡ ´ë±âÁß¿¡ ¹Ù¸®¾î°¡ÆÄ±«µÇ¾úÀ»
°æ¿ì, BrokenBarrierException
À» ¹ß»ý½Ãŵ´Ï´Ù.
´ë±âÁß¿¡ ¸î°³ÀÇ thread·ÎÀÎÅÍ·´Æ®
°¡ ¹ß»ýÇßÀ» °æ¿ì, ´ë±âÁßÀÇ ´Ù¸¥ thread°¡ ¸ðµÎ BrokenBarrierException
¸¦
Throw ÇØ, ¹Ù¸®¾î°¡ °íÀå »óÅ¿¡ ³õ¿©Áý´Ï´Ù.
ÇöÀçÀÇ thread°¡ µµÂøÇÏ´Â ¸¶Áö¸· threadÀ̸ç, »ý¼ºÀÚ³»¿¡¼ nullÀÌ ¾Æ´Ñ ¹Ù¸®¾î ¾×¼ÇÀÌ ÁöÁ¤µÇ´Â °æ¿ì, ÇöÀçÀÇ thread´Â ¾×¼ÇÀ» ½ÇÇàÇÏ°í ³ª¼, ´Ù¸¥ threadÀÇ ¼ÓÇàÀ» Çã¿ëÇÕ´Ï´Ù. ¹Ù¸®¾î ¾×¼ÇÁß¿¡ ¿¹¿Ü°¡ ¹ß»ýÇϸé, ÇöÀçÀÇ thread³»¿¡¼ ±× ¿¹¿Ü°¡ ¼Û½ÅµÇ¾î ¹Ù¸®¾î°¡ °íÀå »óÅ¿¡ ³õ¿©Áý´Ï´Ù.
timeout
- ¹Ù¸®¾î¸¦ ´ë±âÇÏ´Â ½Ã°£unit
- timeout ÆÄ¶ó¹ÌÅÍÀÇ ½Ã°£ ´ÜÀ§
getParties()
- 1 Àº ÃÖÃÊ·Î µµÂøÇÏ´Â thread¸¦, Á¦·Î´Â ¸¶Áö¸·¿¡ µµÂøÇÏ´Â thread¸¦ ³ªÅ¸³½´Ù
InterruptedException
- ´ë±âÁß¿¡
ÇöÀçÀÇ thread·Î ÀÎÅÍ·´Æ®°¡ ¹ß»ýÇßÀ» °æ¿ì
TimeoutException
- ÁöÁ¤µÈ ŸÀÓ ¾Æ¿ô ½Ã°£ÀÌ °æ°úÇßÀ» °æ¿ì
BrokenBarrierException
- ÇöÀçÀÇ threadÀÇ ´ë±âÁß¿¡ ´Ù¸¥ thread·Î ÀÎÅÍ·´Æ®°¡ ¹ß»ýÇÑ, ¹Ù¸®¾î°¡ ¸®¼Â µÈ, await
È£Ãâ½Ã¿¡ ¹Ù¸®¾î°¡ ÆÄ±«µÈ, ¶Ç´Â ¿¹¿Ü¸¦ À§Çؼ ¹Ù¸®¾î ¾×¼Ç (Á¸ÀçÇÏ´Â °æ¿ì)ÀÌ ½ÇÆÐÇßÀ» °æ¿ìpublic boolean isBroken()
public void reset()
BrokenBarrierException
¸¦
ThrowÀÇÇØ º¹±ÍÇÕ´Ï´Ù. ´Ù¸¥ ÀÌÀ¯·Î½á Àý´ÜÀÌ ¹ß»ýÇÑ µÚ¿¡ ¸®¼Â ÇÏ´Â °æ¿ì´Â ½ÇÇàÀÌ º¹ÀâÇÏ°Ô µÇ´Â °æ¿ì°¡ ÀÖ½À´Ï´Ù. Áï, ´Ù¸¥ ¾î¶°ÇÑ ¹æ¹ýÀ¸·Î thread¸¦ À絿±â ÇØ, ¸®¼Â¸¦ ½ÇÇàÇÏ´Â thread¸¦ ¼±ÅÃÇÒ Çʿ䰡 ÀÖ½À´Ï´Ù. ÀÌ·¯ÇÑ »óȲ¿¡¼´Â ÀÌÈÄ¿¡ »ç¿ëÇϱâ À§Çؼ ¹Ù¸®¾î¸¦ »õ·Î¿î ÀÛ¼ºÇÏ´Â (ºÐ)ÆíÀÌ ¹Ù¶÷Á÷ÇÑ °æ¿ì°¡ ÀÖ½À´Ï´Ù.
public int getNumberWaiting()
await()
³»¿¡¼ ÇöÀç ºí·Ï µÇ°í ÀÖ´Â ÆÄƼÀÇ ¼ö
|
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 µµ ÂüÁ¶ÇϽʽÿÀ.