|
JavaTM 2 Platform Standard Ed. 5.0 |
|||||||||
ÀÌÀü Ŭ·¡½º ´ÙÀ½ Ŭ·¡½º | ÇÁ·¹ÀÓÀ¸·Î ÇÁ·¹ÀÓ ¾øÀÌ | |||||||||
°³¿ä: »óÀÚ | Çʵå | constructor | ¸Þ¼µå | »ó¼¼: Çʵå | »ý¼ºÀÚ | ¸Þ¼µå |
java.lang.Objectjava.util.concurrent.locks.ReentrantReadWriteLock
public class ReentrantReadWriteLock
ReentrantLock
¿Í
À¯»çÇÑ ½Ã¸àƽ½º¸¦ Áö¿øÇÏ´Â ReadWriteLock
±¸Çö.
ÀÌ Å¬·¡½º¿¡´Â ´ÙÀ½ÀÇ ÇÁ·ÎÆÛƼ°¡ ÀÖ½À´Ï´Ù.
ÀÌ Å¬·¡½º´Â ¶ô ¾×¼¼½º¿¡ ´ëÇÑ ¸®´õ ¶Ç´Â ¶óÀÌÅÍÀÇ ¿ì¼± ¼ø¼¸¦ ±ÔÁ¤ÇÏÁö ¾Ê½À´Ï´Ù. ´Ù¸¸, À̰ÍÀº ¿É¼ÇÀÇ ¡¸±Õµî¡¹Á¤Ã¥¸¦ Áö¿ø ÇÕ´Ï´Ù. ±ÕµîÀ¸·Î¼ ±¸ÃàµÇ¾úÀ» °æ¿ì, thread´Â ±Ù»çÀÇ µµÂø¼ø¼ Á¤Ã¥¸¦ »ç¿ëÇØ ¿£Æ®¸®·Î °æÇÕ ÇÕ´Ï´Ù. Ãâ·Â ¶ôÀÌ ÇØ¹æµÇ¸é, ´ë±â ½Ã°£ÀÇ °¡Àå ±ä ´ÜÀÏÀÇ Writer¿¡ Ãâ·Â¶ôÀ» ÇÒ´çÇÒ ¼ö ÀÖ´ÂÁö, ¾î´À ¶óÀÌÅͺ¸´Ù ±æ°Ô ´ë±âÇϰí ÀÖ´Â ¸®´õ°¡ Á¸ÀçÇÏ´Â °æ¿ì´Â ¸®´õ¼¼Æ®¿¡ Àб⠶ôÀ» ÇÒ´çÇÒ ¼ö ÀÖ½À´Ï´Ù. ºÒ±ÕµîÀ¸·Î¼ ±¸ÃàµÇ¾úÀ» °æ¿ì´Â ¿£Æ®¸®¸¦ Àá±×´Â ¼ø¼¸¦ µµÂø¼ø¼·Î ÇÒ ÇÊ¿ä´Â ¾ø½À´Ï´Ù. ¾î´À °æ¿ì¿¡µµ, ¸®´õ°¡ ¾×ƼºêÇØ, ¶óÀÌÅͰ¡ ¶ô¿¡ µé¾î°¡´Â °æ¿ì, ±× ¶óÀÌÅͰ¡ Ãâ·Â¶ôÀ» ÃëµæÇØ ÇØ¹æÇÒ ¶§±îÁö, ÈļÓÀÇ ¸®´õ¿¡°Ô´Â Àб⠶ôÀº Çã°¡µÇÁö ¾Ê½À´Ï´Ù.
ÀÌ ¶ôÀº ¸®´õ¿Í ¶óÀÌÅÍÀÇ ¾çÂÊ ¸ðµÎ°¡ReentrantLock
½ºÅ¸ÀÏ·Î Àбâ
¶Ç´Â Ãâ·Â¶ôÀ» Àç¾ò´Â °ÍÀ» Çã¿ëÇÕ´Ï´Ù. Ãâ·ÂÇÏ´Â threadÀÇ º¸°ü À¯ÁöÇÏ´Â Ãâ·Â ¶ôÀÌ ¸ðµÎ ÇØ¹æµÉ ¶§±îÁö, ¸®´õ´Â Çã°¡µÇÁö ¾Ê½À´Ï´Ù.
¶Ç, ¶óÀÌÅÍ´Â Àб⠶ôÀ» ÃëµæÇÒ ¼ö ÀÖ½À´Ï´Ù¸¸, ¸®´õ°¡ Ãâ·Â¶ôÀ» ÃëµæÇÒ ¼ö ¾ø½À´Ï´Ù. ´Ù¸¥ ¾îÇø®ÄÉÀ̼ÇÀÇ »çÀÌ¿¡¼´Â read ¶ôÇÏ¿¡¼ read¸¦ ½ÇÇàÇÏ´Â ¸Þ¼µå¿¡ÀÇ È£ÃâÇØ ¶Ç´Â ÄݹéÁß¿¡ Ãâ·Â¶ôÀÌ º¸°ü À¯ÁöµÇ´Â °æ¿ì, ÀçÀÔ°¡´É¼ºÀº À¯¿ëÇÕ´Ï´Ù. ¸®´õ°¡ Ãâ·Â¶ôÀ» ÃëµæÇÏ·Á°í ÇØµµ, ¼º°øÇÏÁö ¾Ê½À´Ï´Ù.
ÀçÀÔ°¡´É¼ºÀ» ÀÌ¿ëÇϸé, Ãâ·Â ¶ôÀ» ÃëµæÇÏ°í ³ª¼ read ¶ôÀ» ÃëµæÇØ, ±× ÈÄ Ãâ·Â¶ôÀ» ÇØ¹æÇÑ´Ù°í ÇÏ´Â ¹æ¹ýÀ¸·Î Ãâ·Â ¶ôÀ¸·ÎºÎÅÍ Àб⠶ô¿¡ÀÇ °ÝÇϰ¡ °¡´ÉÇÏ°Ô µË´Ï´Ù. ´Ù¸¸, read ¶ôÀ¸·ÎºÎÅÍ Ãâ·Â¶ô¿¡ÀÇ ½Â°ÝÀº ÇÒ ¼ö ¾ø½À´Ï´Ù.
read ¶ô°ú Ãâ·Â¶ôÀÇ ¾çÂÊ ¸ðµÎ°¡ ¶ô ÃëµæÁßÀÇ ÀÎÅÍ·´Æ®¸¦ Áö¿ø ÇÕ´Ï´Ù.
Condition
Áö¿ø
Ãâ·Â ¶ôÀÇ Á¦°øÇÏ´Â Condition
±¸ÇöÀº Ãâ·Â ¶ô¿¡ °ü°èµÇ¾î, ReentrantLock.newCondition()
Á¦°øÇÏ´Â Condition
±¸ÇöÀÌ ReentrantLock
¿¡ ´ëÇØ¼ ½ÇÇàÇÏ´Â °Í°ú °°ÀÌ µ¿ÀÛÇÕ´Ï´Ù. ´ç¿¬ÇÕ´Ï´Ù¸¸, ÀÌ Condition
´Â Ãâ·Â¶ô¿¡¼¸¸ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.
read ¶ôÀº Condition
¸¦
Áö¿ø
ÇÏÁö ¾Ê°í, readLock(). newCondition()°¡ UnsupportedOperationExceptionÀ» Throw ÇÕ´Ï´Ù.
ÀÌ Å¬·¡½º´Â ¶ôÀÌ º¸°ü À¯ÁöµÇ´ÂÁö °æÇÕ ÇÏ´ÂÁö¸¦ ÆÇº°ÇÏ´Â ¸Þ¼µå¸¦ Áö¿ø ÇÕ´Ï´Ù. ÀÌ·¯ÇÑ ¸Þ¼µå´Â µ¿±âÀÇ Á¦¾î¿ëÀ¸·Î¼°¡ ¾Æ´Ï°í, ½Ã½ºÅÛ »óÅÂÀÇ °¨½Ã¿ëÀ¸·Î¼ ¼³°èµÇ°í ÀÖ½À´Ï´Ù.
ÀÌ Å¬·¡½ºÀÇ Á÷·ÄÈ´Â ºôÆ®ÀÎ ¶ô°ú °°°Ô µ¿ÀÛÇÕ´Ï´Ù. Á÷·ÄÈ ÇØÁ¦µÈ ¶ôÀº Á÷·ÄȽà »óÅ¿¡ °ü°è¾øÀÌ, ¶ô ÇØÁ¦ »óŰ¡ µË´Ï´Ù.
»ç¿ë·Ê£º Â÷ÀÇ Äڵ忹¿¡¼´Â ÀçÀÔ°¡´É¼ºÀ» Ȱ¿ëÇØ, ij½¬ÀÇ °»½Å ÈÄ¿¡ ¶ôÀÇ °ÝÇϸ¦ ½ÇÇàÇÏ´Â ¹æ¹ýÀ» ³ªÅ¸³À´Ï´Ù (°£·«ÈÇϱâ À§Çؼ ¿¹¿Ü 󸮴 »ý·« µÇ°í ÀÖ´Ù).
class CachedData { Object data; volatile boolean cacheValid; ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); void processCachedData() { rwl.readLock(). lock(); if (! cacheValid) { // upgrade lock manually rwl.readLock(). unlock(); // must unlock first to obtain writelock rwl.writeLock(). lock(); if (! cacheValid) { // recheck data = ... cacheValid = true; } // downgrade lock rwl.readLock(). lock(); // reacquire read without giving up write lock rwl.writeLock(). unlock(); // unlock write, still hold read } use(data); rwl.readLock(). unlock(); } }ReentrantReadWriteLocks¸¦ »ç¿ëÇØ, ¾î¶² Á¾·ùÀÇ Collections »ç¿ëÀ¸·Î º´Ç༺À» °³¼±ÇÒ ¼ö ÀÖ½À´Ï´Ù. º¸Åë, À̰ÍÀÌ °¡°ªÀÌ ÀÖ´Â °ÍÀº Ä÷º¼ÇÀÌ ´ë±Ô¸ð·Î µÇ´Â °ÍÀÌ ¿¹»óµÇ¾î ¶óÀÌÅÍ threadº¸´Ù ´Ù¼öÀÇ ¸®´õ thread¿¡ ÀÇÇØ ¾×¼¼½º µÇ¾î µ¿±â¿¡ ÇÑ ¿À¹öÇìµå¸¦ ¿ôµµ´Â ¿À¹öÇìµå¸¦ °¡Áö´Â Á¶ÀÛÀÌ Æ÷ÇԵǴ °æ¿ìÀÔ´Ï´Ù. ¿¹·Î¼ ´ë±Ô¸ð¿©, µ¿½Ã ¾×¼¼½º°¡ ¿¹»óµÇ´Â TreeMap¸¦ »ç¿ëÇϴ Ŭ·¡½º¸¦ ÀÌÇÏ¿¡ ³ªÅ¸³À´Ï´Ù.
class RWDictionary { private final Map<String, Data> m = new TreeMap<String, Data>(); private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); private final Lock r = rwl.readLock(); private final Lock w = rwl.writeLock(); public Data get(String key) { r.lock(); try { return m.get(key); } finally { r.unlock(); } } public String[] allKeys() { r.lock(); try { return m.keySet(). toArray(); } finally { r.unlock(); } } public Data put(String key, Data value) { w.lock(); try { return m.put(key, value); } finally { w.unlock(); } } public void clear() { w.lock(); try { m.clear(); } finally { w.unlock(); } } }
ÀçÀÔ °¡´ÉÇÑ Ãâ·Â ¶ôÀº ¼ÒÀ¯ÀÚ¸¦ º»·¡ Á¤ÀÇÇØ¼, ¶ôÀ» ÇØ¹æÇÒ ¼ö ÀÖ´Â °ÍÀº ÃëµæÇÑ thread¸¸ÀÔ´Ï´Ù. ´ëÁ¶ÀûÀ¸·Î ÀÌ ±¸Çö¿¡¼´Â read ¶ô¿¡ ¼ÒÀ¯±ÇÀ̶ó°í ÇÏ´Â °³³äÀº Á¸ÀçÇÏÁö ¾Ê±â ¶§¹®¿¡ read ¶ôÀ» ÇØ¹æÇÏ´Â thread°¡ ±×°ÍÀ» ¾ò´Â thread¿Í °°Áö ¾ÊÀ¸¸é ¾È µÈ´Ù°í ÇÏ´Â °ÍÀº ¾ø½À´Ï´Ù. ´Ù¸¸, ÀÌ ÇÁ·ÎÆÛƼ´Â ÀÌ Å¬·¡½ºÀÇ Àå·¡ÀÇ ±¸ÇöÀ¸·Î º¸°ü À¯ÁöµÇ´Â °ÍÀº º¸ÁõµÇÁö ¾Ê½À´Ï´Ù.
ÀÌ ¶ôÀº ÃÖ´ë 65536
Àç±ÍÀû Ãâ·Â ¶ô ¹× 65536
read ¶ôÀ» Áö¿ø
ÇÕ´Ï´Ù. ÀÌ·¯ÇÑ Á¦ÇÑÀ» ³ÑÀ¸·Á°í Çϸé, Àá±×´Â ¸Þ¼µå·ÎºÎÅÍ Error
°¡ ¹ß»ý µË´Ï´Ù.
Áßø(Nested) Ŭ·¡½º °³¿ä | |
---|---|
static class |
ReentrantReadWriteLock.ReadLock
readLock() ¸Þ¼µå¿¡ ÀÇÇØ ¸®ÅϵÈ
¶ô |
static class |
ReentrantReadWriteLock.WriteLock
writeLock() ¸Þ¼µå¿¡ ÀÇÇØ ¸®ÅϵÈ
¶ô |
»ý¼ºÀÚ °³¿ä | |
---|---|
ReentrantReadWriteLock ()
µðÆúÆ®ÀÇ ¼ø¼ ÇÁ·ÎÆÛƼ·Î »õ·Î¿î ReentrantReadWriteLock¸¦ ÀÛ¼ºÇÕ´Ï´Ù. |
|
ReentrantReadWriteLock (boolean fair)
ÁöÁ¤µÈ ±Õµî¼º Á¤Ã¥¸¦ »ç¿ëÇØ, »õ·Î¿î ReentrantReadWriteLock¸¦ ÀÛ¼ºÇÕ´Ï´Ù. |
¸Þ¼µå °³¿ä | |
---|---|
protected Thread |
getOwner ()
ÇöÀç Ãâ·Â ¶ôÀ» ¼ÒÀ¯Çϰí ÀÖ´Â thread¸¦ ¸®ÅÏÇÕ´Ï´Ù. |
protected Collection <Thread > |
getQueuedReaderThreads ()
read ¶ôÀÇ ÃëµæÀ» ´ë±âÁßÀÇ thread¸¦ Æ÷ÇÔÇÑ Ä÷º¼ÇÀ» ¸®ÅÏÇÕ´Ï´Ù. |
protected Collection <Thread > |
getQueuedThreads ()
read ¶ô ¶Ç´Â Ãâ·Â¶ôÀÇ ¸î°³ÀÇ ÃëµæÀ» ´ë±âÁßÀÇ thread¸¦ Æ÷ÇÔÇÑ Ä÷º¼ÇÀ» ¸®ÅÏÇÕ´Ï´Ù. |
protected Collection <Thread > |
getQueuedWriterThreads ()
Ãâ·Â ¶ôÀÇ ÃëµæÀ» ´ë±âÁßÀÇ thread¸¦ Æ÷ÇÔÇÑ Ä÷º¼ÇÀ» ¸®ÅÏÇÕ´Ï´Ù. |
int |
getQueueLength ()
read ¶ô ¶Ç´Â Ãâ·Â¶ôÀÇ ÃëµæÀ» ´ë±âÁßÀÇ threadÀÇ ÃßÁ¤¼ö¸¦ ¸®ÅÏÇÕ´Ï´Ù. |
int |
getReadLockCount ()
ÀÌ ¶ô¿ëÀ¸·Î º¸°ü À¯ÁöµÇ°í ÀÖ´Â read ¶ôÀÇ ¼ö¸¦ Á¶È¸ÇÕ´Ï´Ù. |
protected Collection <Thread > |
getWaitingThreads (Condition condition)
Ãâ·Â ¶ô¿¡ °ü·ÃµÈ ÁöÁ¤ »óŸ¦ ´ë±âÁßÀÇ thread¸¦ Æ÷ÇÔÇÑ Ä÷º¼ÇÀ» ¸®ÅÏÇÕ´Ï´Ù. |
int |
getWaitQueueLength (Condition condition)
Ãâ·Â ¶ô¿¡ °ü·ÃµÈ ÁöÁ¤ »óÅ·Π´ë±âÁßÀÇ threadÀÇ ÃßÁ¤¼ö¸¦ ¸®ÅÏÇÕ´Ï´Ù. |
int |
getWriteHoldCount ()
ÇöÀçÀÇ thread¿¡ ÇÑ, ÀÌ ¶ô»óÀÇ ÀçÀÔ °¡´ÉÇÑ Ãâ·Â º¸°ü À¯Áö¼ö¸¦ Á¶È¸ÇÕ´Ï´Ù. |
boolean |
hasQueuedThread (Thread thread)
read ¶ô ¶Ç´Â Ãâ·Â¶ôÀÇ ÃëµæÀ» ´ë±âÁßÀÇ ÁöÁ¤ÀÇ thread°¡ Á¸ÀçÇÒÁö ¾î¶³Áö¸¦ Á¶È¸ÇÕ´Ï´Ù. |
boolean |
hasQueuedThreads ()
read ¶ô ¶Ç´Â Ãâ·Â¶ôÀÇ ÃëµæÀ» ´ë±âÁßÀÇ thread°¡ Á¸ÀçÇÒÁö ¾î¶³Áö¸¦ Á¶È¸ÇÕ´Ï´Ù. |
boolean |
hasWaiters (Condition condition)
ÀÌ Ãâ·Â ¶ô¿¡ °ü·ÃµÈ ÁöÁ¤ »óÅ·Π´ë±âÇϰí ÀÖ´Â thread°¡ Á¸ÀçÇÒÁö ¾î¶³Áö¸¦ Á¶È¸ÇÕ´Ï´Ù. |
boolean |
isFair ()
ÀÌ ¶ôÀ¸·Î ±Õµî¼ºÀÌ true ·Î ¼³Á¤µÇ¾î ÀÖ´Â °æ¿ì´Â true¸¦ ¸®ÅÏÇÕ´Ï´Ù. |
boolean |
isWriteLocked ()
Ãâ·Â ¶ôÀÌ thread¿¡ º¸°ü À¯ÁöµÇ°í ÀÖ´ÂÁö¸¦ Á¶È¸ÇÕ´Ï´Ù. |
boolean |
isWriteLockedByCurrentThread ()
ÇöÀçÀÇ thread°¡ ÀÌ Ãâ·Â ¶ôÀ» º¸°ü À¯ÁöÇϰí ÀÖ´Â Á¦¹ßÀ» Á¶È¸ÇÕ´Ï´Ù. |
ReentrantReadWriteLock.ReadLock |
readLock ()
Àб⿡ »ç¿ëÇÏ´Â ¶ôÀ» ¸®ÅÏÇÕ´Ï´Ù. |
String |
toString ()
ÀÌ ¶ô ¹× ±× »óŸ¦ ½Äº°Çϴ ij¸¯ÅÍ ¶óÀÎÀ» ¸®ÅÏÇÕ´Ï´Ù. |
ReentrantReadWriteLock.WriteLock |
writeLock ()
Ãâ·ÂÀ» »ç¿ëÇÏ´Â ¶ôÀ» ¸®ÅÏÇÕ´Ï´Ù. |
Ŭ·¡½º java.lang. Object ·ÎºÎÅÍ »ó¼ÓµÈ ¸Þ¼µå |
---|
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
wait,
wait,
wait |
»ý¼ºÀÚ »ó¼¼ |
---|
public ReentrantReadWriteLock()
public ReentrantReadWriteLock(boolean fair)
fair
- ÀÌ ¶ôÀÌ ±Õµî ¼ø¼ºÎ Á¤Ã¥¸¦ »ç¿ëÇÏ´Â °æ¿ì´Â true¸Þ¼µåÀÇ »ó¼¼ |
---|
public ReentrantReadWriteLock.WriteLock writeLock()
ReadWriteLock
񃬣:
ReadWriteLock
³»ÀÇ writeLock
public ReentrantReadWriteLock.ReadLock readLock()
ReadWriteLock
񃬣:
ReadWriteLock
³»ÀÇ readLock
public final boolean isFair()
protected Thread getOwner()
public int getReadLockCount()
public boolean isWriteLocked()
public boolean isWriteLockedByCurrentThread()
public int getWriteHoldCount()
protected Collection <Thread > getQueuedWriterThreads()
protected Collection <Thread > getQueuedReaderThreads()
public final boolean hasQueuedThreads()
public final boolean hasQueuedThread(Thread thread)
thread
- thread
NullPointerException
- thread°¡ nullÀÎ °æ¿ìpublic final int getQueueLength()
protected Collection <Thread > getQueuedThreads()
public boolean hasWaiters(Condition condition)
condition
- »óÅÂ
IllegalMonitorStateException
- ÀÌ ¶ôÀ» º¸°ü À¯ÁöÇϰí ÀÖÁö ¾Ê´Â °æ¿ì
IllegalArgumentException
- ÁöÁ¤µÈ »óŰ¡ ÀÌ ¶ô°ú °ü·ÃÁöÀ» ¼ö ÀÖÁö ¾ÊÀº °æ¿ì
NullPointerException
- »óŰ¡ nullÀÎ °æ¿ìpublic int getWaitQueueLength(Condition condition)
condition
- »óÅÂ
IllegalMonitorStateException
- ÀÌ ¶ôÀ» º¸°ü À¯ÁöÇϰí ÀÖÁö ¾Ê´Â °æ¿ì
IllegalArgumentException
- ÁöÁ¤µÈ »óŰ¡ ÀÌ ¶ô°ú °ü·ÃÁöÀ» ¼ö ÀÖÁö ¾ÊÀº °æ¿ì
NullPointerException
- »óŰ¡ nullÀÎ °æ¿ìprotected Collection <Thread > getWaitingThreads(Condition condition)
condition
- »óÅÂ
IllegalMonitorStateException
- ÀÌ ¶ôÀ» º¸°ü À¯ÁöÇϰí ÀÖÁö ¾Ê´Â °æ¿ì
IllegalArgumentException
- ÁöÁ¤µÈ »óŰ¡ ÀÌ ¶ô°ú °ü·ÃÁöÀ» ¼ö ÀÖÁö ¾ÊÀº °æ¿ì
NullPointerException
- »óŰ¡ nullÀÎ °æ¿ìpublic String toString()
Object
³»ÀÇ toString
|
JavaTM 2 Platform Standard Ed. 5.0 |
|||||||||
ÀÌÀü Ŭ·¡½º ´ÙÀ½ Ŭ·¡½º | ÇÁ·¹ÀÓÀ¸·Î ÇÁ·¹ÀÓ ¾øÀÌ | |||||||||
°³¿ä: »óÀÚ | Çʵå | constructor | ¸Þ¼µå | »ó¼¼: Çʵå | »ý¼ºÀÚ | ¸Þ¼µå |
Copyright 2004 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms . Documentation Redistribution Policy µµ ÂüÁ¶ÇϽʽÿÀ.