Site Search :
Standard Enterprise XML Methodology Pattern Setting Tunning Other
Article Contributors
GuestBook
Javapattern Maven
XSourceGen Dev
JetSpeed Test
JLook Image
jLook Family Site


NIO Channel
 
½ºÅ͵ð ÀڷḦ ¾ÆƼŬ·Î ¿Ã¸³´Ï´Ù. »õ·Î¿î primitive I/O Ãß»óÈ­ÀÎ ChannelÀ» ÀÌÇØÇϵµ·Ï ÇÑ´Ù. File ChannelÀ» ÅëÇÑ ChannelÀÇ ±¸Çö°ú Ư¡À» ¹è¿ìµµ·Ï ÇÑ´Ù. ºñµ¿±âÀû ÀÔÃâ·ÂÀ» ÀÌÇØÇÏ°í ServerSocketChannel°ú SocketChannelÀ» ÀÌ¿ëÇÏ¿© ÀÌÀü ¹öÀüÀÇ ÀÔÃâ·Â°ú ºñ±³ÇÏ¿©º»´Ù. ( 2003/09/03 ) 223
Written by ienvyou - ÃÖÁö¿õ
1 of 1
 

À̹ø Àå¿¡¼­ ´Ù·ê  ÁÖÁ¦´Â New I/O¶ó°í ºÒ¸®´Â J2SE 1.4¿¡ À־ÀÇ ¸¹Àº º¯°æ »çÇ×µé Áß 
ÀϺÎÀÎ ChannelÀÌ´Ù
¾Õ¿¡¼­ .±âÁ¸ÀÇ Java I/O¿Í ºñ±³ÇÏ¿© BufferÀÇ »ç¿ëÀ¸·Î performance¸¦ Çâ»ó½ÃÅ°°í, garbage¸¦ 
ÁÙÀÏ ¼ö ÀÖ´Ù´Â °ÍÀ» ¹è¿üÀ» °ÍÀÌ´Ù. ChannelÀº ½ÇÁ¦ ÀÛ¾÷ÀÇ ÀÌÇàÀ» À§ÇØ ÀÌ·± buffer¸¦ ÇÊ¿ä·Î 
Çϴµ¥(»ç½Ç, channelÀº buffer¸¦ »ç¿ëÇϱâÀ§ÇÑ ¼ö´ÜÀ̶ó ºÁµµ °ú¾ðÀÌ ¾Æ´Ï´Ù). 

À̹ø Àå¿¡¼­´Â NIOÈ°¿ëÀ» À§ÇØ Channel¿¡ ´ëÇÑ Àü¹ÝÀûÀÎ °³¿ä¿Í Ư¡¿¡ ´ëÇØ  »ìÆ캸µµ·Ï ÇÏ°Ú´Ù.

<½ºÅ͵𳻿ë>

- Channel Basics 
- File Channels
- ServerSocketChannel Ŭ·¡½º¿Í SocketChannel Ŭ·¡½º 
- etc

<½ºÅ͵ð¸ñÇ¥>

- »õ·Î¿î primitive I/O Ãß»óÈ­ÀÎ ChannelÀ» ÀÌÇØÇϵµ·Ï ÇÑ´Ù.
- File ChannelÀ» ÅëÇÑ ChannelÀÇ ±¸Çö°ú Ư¡À» ¹è¿ìµµ·Ï ÇÑ´Ù.
- ºñµ¿±âÀû ÀÔÃâ·ÂÀ» ÀÌÇØÇÏ°í ServerSocketChannel°ú SocketChannelÀ» ÀÌ¿ëÇÏ¿© ÀÌÀü ¹öÀüÀÇ 
ÀÔÃâ·Â°ú ºñ±³ÇÏ¿©º»´Ù.

 

¢º»õ·Î¿î IOÀÇ Ãß»óÈ­ÀΠä³ÎÀÇ Æ¯Â¡-java.nio.channels.Channel

1.1.1 - Channel Basics 

ChannelÀº Çϵå¿þ¾î µð¹ÙÀ̽º, ÆÄÀÏ, ³×Æ®¿öÅ© ¼ÒÄÏ µîÀÌ »óÈ£°£¿¡ Àб⳪ ¾²±â µîÀÇ ÀÔÃâ·Â ÀÛ¾÷À» 
ÇÑ °³ ÀÌ»ó ¼öÇàÇÒ ¼ö ÀÖ´Â Ãß»óÈ­µÈ °èÃþÀÌ´Ù. ¿¹¸¦ µé¸é, java.nio.channels.Channel interface´Â 
¼­ºêÀÎÅÍÆäÀ̽º·ÎºÎÅÍÀÇ Á÷Á¢ÀûÀÎ Àбâ¿Í ¾²±â´Â Áö¿øÇÏÁö ¾Ê°í, ä³Î ÀÎÅÍÆäÀ̽º´Â ´ÙÀ½°ú °°Àº 
¸Þ¼Òµå¸¸À» °®°í ÀÖ´Ù.


    public interface Channel {
      void close() throws IOException;
      boolean isOpen();
    }
±âÁ¸ÀÇ I/O¿Í ºñ±³ÇØ º»´Ù¸é, stream°ú °°´Ù°í ÇÒ ¼ö ÀÖ´Ù. ä³ÎµéÀº ÀÌÀüÀÇ ½ºÆ®¸²-±â¹Ý ¸ðµ¨µé°ú ºñ±³ÇÏ¿´À» ¶§ ¿©·¯ °¡Áö ÀÌÁ¡À» Á¦°øÇÏ°í ÀÖ´Ù:
  1. Ãß»óÀû Ŭ·¡½º º¸´Ù´Â ÀÎÅÍÆäÀ̽º¸¦ »ç¿ëÇÏ´Â È®À强
  2. ä³ÎÀ» ÅëÇØ Àбâ¿Í ¾²±â ´É·ÂÀ» ¸ðµÎ º¸¿©ÁÖ´Â °ÍÀÌ °¡´ÉÇÑ ¾ç¹æÇ⼺ (cf.inputstream,outputstream)
  3. ÆÄÀÏ Àá±Ý°ú ¸Þ¸ð¸® ¸ÅÇÎ µî°ú °°Àº ±â´ÉµéÀÇ Áö¿ø
  4. ºñµ¿±âÀû Ŭ·Î¡°ú Áß´Ü (interruption) À» À§ÇÑ ¸íÈ®ÇÑ ½ºÅ丮 (Áï,NIO ä³ÎÀº ºñµ¿±âÀûÀ¸·Î ´ÝÈ÷°í Áß´ÜµÉ ¼ö ÀÖ¾î,ÇÑ ½º·¹µå°¡ ÇÑ Ã¤³Î¿¡¼­ ÇϳªÀÇ ÀÔÃâ·ÂÀÛ¾÷À¸·Î ºí·ÏÈ­ Çß´Ù¸é ´Ù¸¥ ½º·¹µå°¡ ±× ä³ÎÀ» ´ÝÀ» ¼öÀÖ´Ù´Â °ÍÀÌ´Ù)
  5. ¹öÆÛ °´Ã¼µé°úÀÇ ±ä¹ÐÇÑ ÅëÇÕ
óÀ½ÀÇ ³× °¡Áö ±â´Éµéµµ Áß¿äÇϱâ´Â ÇÏÁö¸¸ ½ÇÁ¦·Î ¼¼ ¹ø°I/O µ¥ÀÌÅÍ ¸ðµ¨ÀÇ »ý¼ºÀ» Á¤´çÈ­ÇÏ´Â °ÍÀº ¹Ù·Î ¸¶Áö¸· °ÍÀÌ´Ù. ¹öÆÛ´Â New I/OÀÇ Çâ»óµÈ ±â´É¿¡ À־ Áß¿äÇÑ ¿ªÇÒÀ» ÇÏ°í, ä³ÎµéÀº ¹öÆÛÀÇ Àбâ¿Í ¾²±â¸¦ À§ÇØ ±¸ÃàµÈ °´Ã¼µéÀÌ´Ù. (NIOÆÐÅ°Áö¿¡¼­ ¸ðµç µ¥ÀÌÅÍ´Â ¹öÆÛ¸¦ »ç¿ëÇØ ´Ù·ç¾îÁø´Ù.Áï, byte¸¦ Á÷Á¢ channel¿¡ ¾²°Å³ª,ÀÐÀ» ¼ö ¾ø°í, byteµéÀ» ´ãÀº buffer¸¦ »ç¿ëÇØ¾ß ÇÑ´Ù. ) ±×¸² 1¿¡ º¸¿©Áö´Â °èÃþµµ¿¡¼­¿Í °°ÀÌ java.nio.channels ÆÐÅ°Áö´Â ¿©¼¸ °¡ÁöÀÇ ÁÖ¿ä ÀÎÅÍÆäÀ̽º¸¦ º¸À¯ÇÏ°í ÀÖ´Ù ¢ºByteChannel interface

public interface ReadableByteChannel extends Channel {
  int read(ByteBuffer dst) throws IOException;
}
public interface WritableByteChannel extends Channel {
  int write(ByteBuffer src) throws IOException;
}
public interface ByteChannel extends ReadableByteChannel,
 WritableByteChannel {
  // »õ·Î¿î ¸Þ¼Òµå ¾øÀ½
}
¢ººÐ»ê°ú ÁýÇÕ (Scattering and Gathering) ScatteringByteChannel interface°ú GatheringByteChannel interface --> ¿©·¯°³ÀÇ ¹öÆÛ¸¦ ÀоîµéÀ̰ųª ¾²´Â °ÍÀ» Áö¿øÇÑ´Ù. ¤± public long read(ByteBuffer [] dsts) throws IOException ¤± public long read(ByteBuffer [] dsts, int offset, int length) throws IOException ¤± public long write(ByteBuffer [] srcs) throws IOException ¤± public long write(ByteBuffer [] srcs, int offset, int length) throws IOException ºÐ»ê (scattering)"À̶ó´Â À̸§Àº ´ÜÀÏ Ã¤³ÎÀÇ µ¥ÀÌÅÍ°¡ ´ÙÁß ¹öÆÛ¿¡ ÆÛÁ® ÀÖ´Ù´Â »ý°¢¿¡¼­ ºñ·ÔµÈ °ÍÀÌ´Ù. ÀÌ ¹æ½ÄÀº ÀϺΠÇÁ·ÎÅäÄÝ ¶Ç´Â ÆÄÀÏ Æ÷¸ËÀÇ °æ¿ì ¸Å¿ì °£ÆíÇÏ°Ô »ç¿ëÇÒ ¼ö Àִµ¥, ¿¹¸¦ µé¸é, »ç¿ëÀÚ´Â °íÁ¤µÈ ±æÀÌÀÇ À̹ÌÁö Çì´õ¸¦ ÇϳªÀÇ ¹öÆÛ·Î ÀÐ°í ±×°ÍÀÇ ÄÁÅÙÆ®´Â ´Ù¸¥ °ÍÀ¸·Î ÀÐÀ» ¼ö°¡ ÀÖ´Ù. ä³ÎÀº ´ÙÀ½À» ÁøÇàÇϱâ Àü¿¡ ¿¬¼ÓÀûÀ¸·Î À§Ä¡ÇÑ °¢°¢ÀÇ ¹öÆÛ¸¦ ä¿ì·Á°í ³ë·ÂÀ» ÇÏ°Ô µÇ´Âµ¥, ¾î´À ¹öÆÛ°¡ ½ÃÀÛµÇ°í ¾ó¸¶³ª ¸¹Àº ¹öÆÛµéÀ» ä¿ï ¼ö ÀÖ´ÂÁö¸¦ ³ªÅ¸³»±â À§Çؼ­´Â µÎ ¹ø° ¸Þ¼Òµå¿¡¼­ ¿ÀÇÁ¼¼Æ®¿Í ±æÀ̸¦ Ç¥½ÃÇÏ´Â °ÍÀÌ ¸Å¿ì Áß¿äÇÏ´Ù. ±×°ÍµéÀº µ¥ÀÌÅ͸¦ À§Ä¡ ½Ãų ¹öÆÛ ³»ÀÇ Àå¼Ò¿Í´Â ÀüÇô °ü·ÃÀÌ ¾ø°í, ¹öÆÛµéÀº Ç×»ó ±×°Íµé ÀÚ½ÅÀÇ ³»ºÎ »óŸ¦ º¸Á¸ÇÏ°í ÀÖ´Ù. GatheringByteChannel ÀÎÅÍÆäÀ̽º´Â ºÐ»êÀÇ Ä«¿îÅÍÆÄÆ® ¿ªÇÒÀ» ÇÑ´Ù. ¿©·¯ºÐÀÌ ¿¹»óÇß´ø °Íó·³ ÁýÇÕ(gathering) ä³ÎÀº µ¥ÀÌÅÍ Ã¤³ÎÀ» ÀÛ¼ºÇÏ´Â ¿¬¼ÓµÈ ¹öÆÛµé·ÎºÎÅÍ ¼öÁýµÇ°í ÀÖ´Ù. ¶ÇÇÑ ÀÌ ¹æ¹ýÀº ÀϺΠÇÁ·ÎÅäÄÝ ¶Ç´Â ÆÄÀÏ Æ÷¸ËÀÇ °æ¿ì¿¡´Â ¸Å¿ì À¯¿ëÇÏ°Ô »ç¿ëµÇ°í ÀÖ´Ù. ¿¹¸¦ µé¸é, ¿©·¯ºÐÀº (³× °³ÀÇ ±¸ºÐµÈ ¹öÆÛ¿¡ ÀúÀåµÈ) »óÅ ÄÚµå ¶óÀÎ, ÀϺΠǥÁØ ¼­¹ö Çì´õ, »ç¿ëÀÚ-ÇÒ´ç Çì´õ ±×¸®°í ÀÀ´ä º»¹® µîÀ¸·Î ÀÌ·ç¾îÁø HTTP ÀÀ´äÀ» ±¸¼ºÇÒ ¼ö ÀÖ´Ù. ÀÀ´ä º»¹®Àº ´ë¿ë·® ¸Þ¸ð¸®-¸ÅÇÎµÈ ÆÄÀÏ ¹öÆÛ ³»¿¡ À§Ä¡ÇÒ ¼ö ÀÖ°í, ¼­¹ö Çì´õ´Â Ä¿³Î ¸Þ¸ð¸®¿¡ ÀúÀåµÉ ¼ö ÀÖÀ¸¸ç, ¼öÁý ä³ÎÀº ´ÙÀ½À» ÁøÇàÇϱâ Àü¿¡ °¢°¢ÀÇ ¹öÆÛ Ã¤³Î·Î ÀÛ¼ºÀ» ÇÏ°í ä³ÎÀº ÅëÇյǾî Á¤·ÄµÈ Ãâ·ÂÀ» º¸°Ô µÉ °ÍÀÌ´Ù. ====== <¿©±â¼­ Àá±ñ!!> ============ "scattering/gatheringÀÇ ¸ñÀûÀº ¹«¾ùÀϱî? °£ÆíÇÔ¿¡µµ ºÒ±¸ÇÏ°í ¿Ö ÇÁ·Î±×·¡¸Ó´Â for ·çÇÁ¸¦ ÀÛ¼ºÇÒ ¼ö°¡ ¾øÀ»±î? ¿Ö for ·çÇÁ¸¦ ÇÇÇÏ´Â ÀÎÅÍÆäÀ̽º °èÃþÀº º¹ÀâÇÑ °ÍÀϱî?"¶ó°í Áú¹®ÇÒ ¼ö ÀÖ´Ù. ¾à 20ÃÊ µ¿¾È ÀÌ°Í¿¡ ´ëÇØ Çѹø »ý°¢ÇØ º» ÈÄ ÇØ´äÀ» Àо¾Æ¶ó. ºÐ»ê/ÁýÇÕÀÇ ¸ñÀûÀº ÇÁ·Î±×·¡¸ÓÀÇ È¿À²¼ºÀÌ ¾Æ´Ï´Ù; ±×°ÍÀº ½Ã½ºÅÛÀÇ È¿À²¼ºÀÌ´Ù. ¿î¿µ üÁ¦´Â »ç¿ëÀÚ °ø°£¿¡¼­ ½Ã½ºÅÛ °ø°£À¸·Î Á¡ÇÁÇÏ´Â ½Ã½ºÅÛÀÇ ¹Ýº¹ È£Ãâ º¸´Ù´Â ¿ÀÈ÷·Á ´ÜÀÏ ½Ã½ºÅÛ È£ÃâÀÇ °á°ú·Î ¹öÆÛ¸¦ °¡µæ ä¿ì°Å³ª °í°¥½Ãų ¼ö ÀÖ´Ù. ¶ÇÇÑ ´ÙÁß CPU ½Ã½ºÅÛ µî°ú °°Àº ÀϺΠ½Ã½ºÅ۵鵵 µ¿½Ã¿¡ °¡µæ ä¿ì°Å³ª °í°¥½Ãų ¼ö ÀÖ´Ù. ¿¹¸¦ µé¸é, SCSI´Â ½Ã½ºÅÛ¿¡ ÀÛ¼ºÀ» ÇÒ ¶§ ºÐ»ê/ÁýÇÕÀ» Áö¿øÇÏ°í ÀÖ½À´Ï´Ù. ±× ½Ã½ºÅÛÀÇ ±â´É¿¡ µû¶ó ºÐ»ê/ÁýÇÕÀº for ·çÇÁ¿¡ ´ëÇÑ ¸·´ëÇÑ ¼º´ÉÀÇ Çâ»óÀ» ÀÌ·ê ¼ö°¡ ÀÖ´Ù. ÀÌ°ÍÀ» ÀÌÇØÇÑ´Ù¸é ¿©·¯ºÐÀº »õ·Î¿î I/OÀÇ °³³äµµ ÀÌÇØÇÒ ¼ö ÀÖÀ» °ÍÀÌ´Ù. ´ëºÎºÐÀÇ Java¿Í´Â ´Ù¸£°Ô ÀÌ °üÁ¡Àº ¿©·¯ºÐÀÇ »ýÈ°À» ´õ ½±°Ô ¸¸µéÁö´Â ¾Ê´Â´Ù (°¡²ûÀº ±×·¯ÇÏÁö¸¸) ±×°ÍÀº ½Ã½ºÅÛÀÇ »ýÈ°À» ´õ ½±°Ô ¸¸µå´Â °ÍÀÌ´Ù. New I/O´Â ³ôÀº ¼öÁØÀÇ Java °´Ã¼µéÀ» »ç¿ëÇؼ­ ³·Àº ¼öÁØÀÇ ½Ã½ºÅÛ ±â´ÉÀ» ±×´ë·Î µå·¯³½´Ù. ' »ç½Ç, ³ª´Â ÀÌ scattering°ú gatheringÀÌ ÀÚ¹Ù¿¡ ÀÖ¾î ¾î¶² Å« ¿µÇâÀÌ ÀÖÀ»±î¸¦ È®½ÇÈ÷ ÀÌÇØ ÇÒ ¼ø ¾ø¾ú´Ù. ÇÏÁö¸¸, Á¤¸®Çغ¸°Çµ¥, scattering°ú gathering¼öÇàÀº ¼ö³â°£ ¿î¿µÃ¼°è¿¡¼­ È¿°úÀûÀÎ I/O¸Å´ÏÁ® ¿ªÇÒÀ» ÇØ¿Ô´ø °ÍÀ̸ç, ÀÚ¹Ù¿¡¼­ channelÀÇ ÀÌ·± ºÐ»ê,ÁýÇÕ±â´ÉÀÌ ½Ã½ºÅÛ¿µ¿ª±îÁö ±í¼÷ÀÌ »ç¿ëµÉ¼ö ÀÖÀ¸´Ï, Áß¿äÇÑ º¯È­ÀÌ´Ù ´Â °Í°°´Ù.(ÀÌ °³³äÀÌ »ý°¢º¸´Ù, Å« °³³äÀ¸·Î ³ª¿Í¼­ ÀÌÇØÇÏ·Á°í ³ë·ÂÇ߱⿡..--;; Âü°í·Î ºÙÀΰÍÀÌ´Ù.Á»´õ ¸íÄèÇÑ Çؼ® ÀÌ µÇ½Ã´Â ºÐÀº ¾Ë·ÁÁֽöó~~) (Âü°í : http://otn.oracle.co.kr/tech/column/2003/hunteronj2se.jsp) ¢ºInterruptibleChannel ÀÎÅÍÆäÀ̽º ºí·ÎÅ·µÈ ½º·¹µå(ÀÔÃâ·Â)¸¦ ±ú¿ì´Â ±â´É ==> non-blocking ±â´É ä³Î ÀÔÃâ·Â Áß ´Ù¸¥ ½º·¹µå¿¡ ÀÇÇØ Ã¤³ÎÀÌ Áß´ÜµÉ ¼ö ÀÖÀ½À» ³ªÅ¸³»´Â ÀÎÅÍÆäÀ̽ºÀÌ´Ù New I/OÀÇ ¸ðµç ä³Î Ŭ·¡½ºµéÀº ÀÌ ÀÎÅÍÆäÀ̽º¸¦ ±¸ÇöÇÏ°í ÀÖÀ¸¹Ç·Î ¸ðµç ä³ÎÀº blocking Áß¿¡ ´Ù¸¥ ½º·¹µå¿¡ ÀÇÇØ ±ú¾î³¯ ¼ö ÀÖ´Â °ÍÀÌ´Ù. ÀÌ°ÍÀº Áß¿äÇÑ Æ¯¼ºÀ¸·Î ÆÄÀÏ󸮻Ӹ¸ ¾Æ´Ï¶ó ³ªÁß¿¡ ³×Æ®¿öÅ©¿¡ °üÇÑ »çÇ×À» ´Ù·ê¶§ ³ª¿À´Â Selector¿¡ °ü·ÃµÈ ³»¿ëÀ̱⵵ ÇÏ´Ù. ¿ì¼±Àº ä³ÎµéÀº ¾î¶»°Ô ±¸ÇöµÇ´øÁö °£¿¡ Ç×»ó ½º·¹µå-¼¼ÀÌÇÁÀÌ°í µ¿½Ã ¹ß»ýÀÇ Àбâ¿Í ¾²±â°¡ ¿¬¼ÓÀûÀ¸·Î ¼öÇàµÈ´Ù´Â °ÍÀ» Ç×»ó º¸ÀåÀ» ÇÏ°í ÀÖ´Ù°í ¾Ë¾ÆµÎÀÚ. (µÚ¿¡ ÇØ´ç ¿¹¸¦ ¼³¸íÇÒ FileChannel, ServerSocketChannel, SocketChannel ¼¼°³´Â ²À ±â¾ïÇصνöó~) ====== <Âü°íÇϼ¼¿ä> InterruptibleChannel ÀÎÅÍÆäÀ̽º ============ ¿ä ÀÎÅÍÆäÀ̽º´Â NIOÀÇ Áß¿äÇÑ ºñµ¿±âÀû ÀÔÃâ·Â¿¡ Á߿俪ÇÒÀÌ´Ï, ¾î¶»°Ô ±¸ÇöµÇ´ÂÁö´Â ¾Ë¾Æ¾ß ÇÒ °Í°°¾Æ Á» ¹«½ÄÇÑ °Í°°Áö¸¸,APIºÎºÐÀ» ¿Å°ÜµÐ´Ù. public interface InterruptibleChannel extends Channel A channel that can be asynchronously closed and interrupted. A channel that implements this interface is asynchronously closeable: If a thread is blocked in an I/O operation on an interruptible channel then another thread may invoke the channel's close method. This will cause the blocked thread to receive an AsynchronousCloseException. A channel that implements this interface is also interruptible: If a thread is blocked in an I/O operation on an interruptible channel then another thread may invoke the blocked thread's interrupt method. This will cause the channel to be closed, the blocked thread to receive a ClosedByInterruptException, and the blocked thread's interrupt status to be set. If a thread's interrupt status is already set and it invokes a blocking I/O operation upon a channel then the channel will be closed and the thread will immediately receive a ClosedByInterruptException; its interrupt status will remain set. A channel supports asynchronous closing and interruption if, and only if, it implements this interface. This can be tested at runtime, if necessary, via the instanceof operator. - ¾²·¹µåÀÇ Á¦¾î¹æ¹ýÀ» ÀÌ¿ëÇÑ °ÍÀ̶õ °ÍÀ» ¾Ë ¼öÀÖÀ¸¸®¶ó º»´Ù. ¢ºSelectableChannel Ŭ·¡½º (interruptibleChannel±¸Çö) Selector¿¡ ÀÇÇÑ °ü¸®³ª Non-Blocking I/O¸¦ À§ÇÑ ±âº»ÀûÀÎ ±â´ÉÀ» °¡Áø Abstract classÀÌ´Ù. SelectableChannelÀº SelectorŬ·¡½º¿¡ ÀÇÇØ ¼±ÅÃµÉ ¼ö ÀÖÀ¸¸ç Non-Blocking I/O¸¦ °¡´ÉÇÏ°Ô ÇØÁØ´Ù. °¡´ÉÇÑ ÀÌÀ¯´Â AbstractInterruptibleChannel Ŭ·¡½º¸¦ »ó¼ÓÇϱ⠶§¹®ÀÌ´Ù. selectableChannelÀº ºí·Ï¸ðµå³ª ºñºí·Ï¸ðµå°¡ µÇ´Âµ¥ , »õ·Ó°Ô ÀÛ¼ºµÇ¸é, Ç×»ó blocking mode°¡ µÈ´Ù. Non -Blocking mode´Â seletorÀÇ ´ÙÁßÈ­¿Í ÇÔ²² °¡Àå À¯¿ëÇѵ¥, Selector°¡ °ü¸®ÇÏ´Â SelectableChannelµéÀº ÀüºÎ non-blocking I/O¸ðµåÀ̾î¾ß Çϸç blocking I/O¸ðµåÀÇ Ã¤³ÎÀº Selector¿¡ ÀÇÇØ °ü¸®µÇÁö ¸øÇÑ´Ù. (selector¿¡ °üÇؼ­´Â µÞÆÄÆ®¿¡ ÀÚ¼¼È÷ ³ª¿Ã°ÍÀÌ´Ù) 1.1.2. FileChannel FileChannels´Â ÆÄÀÏÀÔÃâ·ÂÀ» À§ÇÑ Ã¤³Î·Î AbstractInterruptibleChannel Ŭ·¡½º¸¦ »ó¼ÓÇؼ­ ºñµ¿±âÀûÀ¸·Î Áß´ÜµÉ ¼ö ÀÖ°Ô µÇ¾îÀÖ´Ù. ±×¸®°í ByteChannel ÀÎÅÍÆäÀ̽º¸¦ ±¸ÇöÇؼ­ Àбâ¿Í ¾²±â¸¦ µ¿½Ã¿¡ ÇÒ ¼ö ÀÖ´Ù. ¾Õ¿¡¼­ °èÃþµµµî º¹ÀâÇÑ °Íó·³ ¸»ÇßÁö¸¸,ChannelÀ» »ý¼ºÇÏ´Â °ÍÀº °£´ÜÇÏ´Ù. I/OÀÇ °¡Àå ±âº»ÀÌ µÇ´Â ÆÄÀÏ¿¡ Àбâ,¾²±â¸¦ Çغ¸°Ú´Âµ¥, ¿ì¼±,±âÁ¸ÀÇ I/OÀÇ file 󸮸¦ »ç¿ëÇÏ¿© (StreamÀÌ µé¾î°¡´Â Ŭ·¡½º°¡ »ý °¢³ªÁö ¾Ê´Â°¡?) ¾ò¾î¿À¸é µÈ´Ù.

public class CopyFile
{
  static public void main( String args[] ) throws Exception {
    if (args.length<2) {
      System.err.println( "Usage: java CopyFile infile outfile" );
      System.exit( 1 );
    }
    String infile = args[0];
    String outfile = args[1];
    FileInputStream fin = new FileInputStream( infile );
    FileOutputStream fout = new FileOutputStream( outfile );
    FileChannel fcin = fin.getChannel(); //channel¾ò±â
    FileChannel fcout = fout.getChannel();
    ByteBuffer buffer = ByteBuffer.allocate( 1024 ); //¹öÆÛÇü¼º
    while (true) {
      buffer.clear();
      int r = fcin.read( buffer ); //channelÅëÇØ buffer Àбâ
      if (r==-1) {        break;      }
      buffer.flip();
      fcout.write( buffer ); //channelÅëÇØ buffer ¾²±â
   }
  }
} 
¾Õ¿¡¼­ ÇнÀÇÑ Buffer ¿¡¼­ ¸¹ÀÌ º¸¾ÒÀ¸´Ï,´«¿¡ ÀÍÀ¸¸®¶ó º»´Ù. À§´Â °¢ ½ºÆ®¸² Ŭ·¡½º¿¡ ä³Î·ÎÀÇ º¯È¯À» Áö¿øÇÏ´Â ¸Þ¼Òµå¸¦ »ç¿ëÇÑ ¹æ¹ýÀÌ°í, java.nio.channels.Channels¶ó´Â À¯Æ¿¸®Æ¼Å¬·¡½º¿¡ Àִ ä³Î·ÎÀÇ º¯È¯À» Áö¿øÇÏ´Â static ¸Þ¼Ò µå¸¦ »ç¿ëÇصµ µÈ´Ù. ¶Ç,ÀÌ java.nio.channels.Channels¿¡´Â ä³Î¿¡¼­ ½ºÆ®¸²À¸·Î º¯È¯ÇÏ´Â ¸Þ¼Òµåµµ ÀÖ´Ù.(½ºÆ®¸²Àº ´Ü¼øÇÑ µ¥ÀÌÅÍÈ帧ÀÌÁö¸¸, ä³ÎÀº ´õ Ãß»óÈ­µÈ ´Ü°è·Î Áú¼­°¡ ºÎ¿©µÈ µ¥ÀÌÅÍ È帧À» ³ªÅ¸³½´Ù°í ÇÒ ¼ö ÀÖ´Ù.) ¢ºFile Lock ÇÁ·Î±×·¥À̳ª À¯Àúµé¿¡°Ô¼­ ƯÁ¤ÆÄÀÏ¿¡ Á¢±ÙÀ» ¹æÁöÇÏ´Â °Íó·³ »ý°¢µÇ°ÚÁö¸¸, file lockÀº º¸Åë ÀÚ¹Ù°´Ã¼ÀÇ lockÀÇ ÀÇ¹Ì¿Í °°´Ù.(advisory lock) Áï,¾î¶² µ¥ÀÌÅÍÁ¢±Ùµµ ¹æÁöÇÒ ¼ö ¾øÁö¸¸, OS¿¡¼­ Áö¿øÇÏ´Â ÆÄÀÏ°£ lockÀ» °ø½ÄÀûÀ¸·Î Áö¿øÇÑ´Ù.À̸¦ »ç¿ëÇÏ¸é ºÒ¸íÈ®ÇÑ ÀÌÀ¯¿¡ ÀÇÇØ ÆÄÀÏ ÀÔÃâ·Â¿¡ Á¦¾à¹Þ´Â »óȲÀ» ÇÇÇÒ ¼ö ÀÖ°Ô µÇ¸é È¿À²ÀûÀ¸·Î ÆÄÀÏ¿¡ ¶ôÀ» °É°Å³ª ÇØÁ¦ÇÒ ¼ö ÀÖ°Ô µÈ´Ù.

RandomAccessFile raf = new RandomAccessFile( "usefilelocks.txt", "rw" );
    FileChannel fc = raf.getChannel();
   // Get lock
     FileLock lock = fc.lock( 10, 20, false ); //ºÎºÐÀûÀÎ lock¼³Á¤
Lock()È£Ãâ½Ã ÀÌ ÆÄÀÏÀ» lockingÇÏ´Â ´Ù¸¥ ¾²·¹µå°¡ ¾ø´Â ÇÑ ¶ôÀ» °É ¼ö ÀÖ´Ù. ¸¸¾à ´Ù¸¥ ½º·¹µå°¡ ÀÌ ÆÄÀÏÀ» ¶ôÅ·ÇÏ°í ÀÖ´Ù¸é ¶ôÀÌ Ç®¸±¶§±îÁö ±â´Ù¸®°Ô µÈ´Ù. ¶ôÀ» °øÀ¯ÇÑ´Ù¸é ÇϳªÀÇ ¾²·¹µå¸¸ µ¶Á¡ÀûÀ¸·Î »ç¿ëÇÏ´Â °ÍÀÌ ¾Æ´Ï¶ó ¹Ì¸® ¾à¼ÓµÈ ¾²·¹µå¿¡ ÇÑÇØ ±× ÆÄÀÏ¿¡ Á¢±Ù °¡´ÉÇÏ´Ù. (OSÀÇ lockÁ¤Ã¥¿¡ Á¿ìµÈ´Ù) ¢ºMemory-mapped file I/O Memory-mapped fiel I/O´Â ±âÁ¸ÀÇ ½ºÆ®¸²À̳ª ´Ù¸¥ ä³Î I/Oº¸´Ù ´õ ºü¸¦ ¼ö ÀÖ´Ù. (MappedByBuffer¿¡¼­ ´Ù·ç¾îÁ³À» °ÍÀÌ´Ù) 1.1.3 ServerSocketChannel Ŭ·¡½º¿Í SocketChannel Ŭ·¡½º À̵éÀº netÆÐÅ°ÁöÀÇ ServerSocketŬ·¡½º¿Í SocketŬ·¡½º¸¦ ä³Î·Î¼­ ´Ù·ç°íÀÚ ÇÒ ¶§ ¾²´Â SelectableChannelÀÌ´Ù. ¾Õ¿¡¼­ selectableChannelÀÌ non-blocking±â´É°ú ÇÔ²² ³ª¿Ô´ø °ÍÀ» ±â¾ïÇϽô°¡? ÀÌµé ³×Æ®¿öÅ© °ü·Ã ä³ÎµéÀº µ¶ÀÚÀûÀ¸·Î ¼ÒÄÏÀÇ ¿ªÇÒÀ» ´ëóÇÏÁö´Â ¾Ê´Â´Ù. ´ë½Å ¼ÒÄÏ Å¬·¡½º¸¦ ³»ºÎ¿¡ °¡Áö°í ÀÖÀ¸¸é¼­ À̵éÀÇ ±â´ÉÀ» ä³ÎÈ­Çϴµ¥ ÀûÀýÈ÷ ÀÌ¿ëÇÏ°Ô µÈ´Ù. ¢ºNew working and asynchronous I/O ³×Æ®¿öÅ·Àº ÀÚ¹Ù¿¡¼­ I/O°úÁ¤ÀÇ ÇʼöÁö½ÄÀÎ ºñµ¿±â½Ä ÀÔÃâ·ÂÀ» ¹è¿ì´Â À§ÇÑ ÁÁÀº ºÐ¾ßÀÌ´Ù. NIO¿¡¼­ networkingµµ ¾Õ¿¡¼­ ¼³¸íÇÑ NIOÀÇ ´Ù¸¥ µ¿ÀÛ°ú Å©°Ô ´Ù¸£Áö ¾Ê´Ù. ºñµ¿±â½Ä ÀÔÃâ·ÂÀº ÇѸ¶µð·Î,blocking±â´É ¾øÀÌ µ¥ÀÌÅ͸¦ Àаųª ¾²´Â ¹æ¹ýÀ» ¸»ÇÑ´Ù. º¸Åë read()¸¦ È£ÃâÇϸé,±× ÇØ´ç ÄÚµåºí·°Àº µ¥ÀÌÅÍ°¡ ´Ù ÀÐÇôÁú¶§±îÁö, ºí·ÏµÈ´Ù. ¹Ý¸é¿¡,ºñµ¿±âÀû ÀÔÃâ·ÂÀÌ È£ÃâµÇ¸é ºí·ÏµÇÁö ¾Ê´Â´Ù. ±×´ë½Å, µ¥ÀÌÅÍ°¡ ¿ÏÀüÈ÷ ÀÐÇôÁø´Ù´ø°¡ »õ·Î¿î ¼ÒÄÏ¿¬°á °°Àº ƯÁ¤ I/OÀ̺¥Æ®¸¦ µî·ÏÇØ µÎ¾ú´Ù°¡ ±×·¯ÇÑ À̺¥Æ®°¡ ÀϾ¸é ±×°ÍÀ» ¾Ë·ÁÁØ´Ù. ºñµ¿±âÀû ÀÔÃâ·ÂÀÇ ÀåÁ¡Áß Çϳª´Â ¸Å¿ì ¸¹Àº ÀÔÃâ·ÂÀÌ µ¿½Ã¿¡ ¸¹ÀÌ ÀÏ¾î ³ª´õ¶óµµ,¶Ç ´Ù¸¥ I/OÀÛ¾÷À» ÇÒ¼ö ÀÖ´Ù´Â °ÍÀÌ´Ù. µ¿±âÀû ÇÁ·Î±×·¥Àº ÀÚÁÖ °è¼Ó pollingÇϰųª ¸¹Àº ¿¬°áÀ» ÇØ°áÇϱâ À§ÇØ ¸Å¿ì ¸¹Àº ¾²·¹µå¸¦ ¸¸µé¾î¾ß ÇÑ´Ù. ºñµ¿±âÀû ÀÔÃâ·Â¿¡¼­´Â ÀÌ °°Àº pollingÀ̳ª ¿©ºÐÀÇ ¾²·¹µå»ý¼º¾øÀÌ ÀÓÀÇÀÇ ÀûÀýÇÑ ¼ýÀÚÀÇ channel¿¡¼­ I/OÀ̺¥Æ®¸¦ °¨½ÃÇÒ¼ö ÀÖ´Ù. ¸¹Àº ¿¹·Î È°¿ëµÇ´Â 1.4ÀÌÀüÀÇ °£´ÜÇÑ ¼­¹ö¼ÒÄÏ ÇÁ·Î±×·¥°ú ºñ±³ÇÏ¿© ¾Ë¾Æº¸ÀÚ. ServerSocket s = new ServerSocket(); //¼­¹ö¼ÒÄÏ»ý¼º Socket conn = s.accept( ); // »õ·Î¿î ¿¬°á ¼ÒÄÏ accept() È£ÃâÀº ¼­¹ö ¼ÒÄÏÀÌ Å¬¶óÀ̾ðÆ®ÀÇ ¿¬°á ¿äûÀ» ¹Þ¾ÆµéÀÏ ¶§±îÁö ºí·ÏÈ­µÈ´Ù. ÀÌ°ÍÀº È£ÃâÇÏ´Â ¾²·¹µå°¡ °áÁ¤µÇÁö ¾Ê´Â ½Ã°£µ¿¾È ¸ØÃß¾î Àִµ¥, ÀÌ ¾îÇø®ÄÉÀ̼ÇÀÌ ´ÜÁö ÇϳªÀÇ ¾²·¹µå¶ó¸é ½Ã½ºÅÛ¿¡ Å« Ÿ°ÝÀÌ´Ù. ÀÏ´Ü ¿¬°áÀÌ ÀÌ·ç¾îÁö¸é ¼­¹ö°¡ ¼ÒÄÏÀ¸·ÎºÎÅÍ LineNumberReader¸¦ »ç¿ëÇØ Å¬¶óÀ̾ðÆ® ¿äûÀ» Àд´Ù. LineNumberReader´Â ¹öÆÛ°¡ °¡µæÂû ¶§±îÁö µ¥ÀÌÅ͸¦ Å« ´ÜÀ§·Î ÀÐ¾î ³ª°¡±â ¶§¹®¿¡ È£ÃâÀº Àб⠻óÅ¿¡¼­ ºí·ÏÈ­µÈ´Ù. ´ÙÀ½ ¿¹Á¦ ÄÚµå´Â ½ÇÇàÁßÀÎ LineNumberReader¸¦ º¸¿©ÁØ´Ù. (ºí·Ï°ú ¸ðµç °Í)

InputStream in = conn.getInputStream();
InputStreamReader rdr = new InputStreamReader(in);
LineNumberReader lnr = new LineNumberReader(rdr);
Request req = new Request();
while (!req.isComplete() )
{
   String s = lnr.readLine();//SocketInputStream.rea()À» È£Ãâ
   req.addLine(s);
}
ÀÌ ÄÚµå´Â ¹®Á¦¸¦ ÀÏÀ¸Å²´Ù. µ¥ÀÌÅÍ´Â ³×Æ®¿öÅ© buffer¿¡¼­ ±â´Ù¸®°í È£ÃâÇÑ°÷¿¡ µ¥ÀÌÅÍ°¡ ¸®ÅϵǴµ¥,ÃæºÐÇÑ µ¥ÀÌÅÍ°¡ ¹öÆÛµÇÁö ¾ÊÀ¸¸é Àбâ À§ÇÑ È£ÃâÀº ÃæºÐÇÑ µ¥ÀÌÅÍ°¡ ¹Þ¾ÆÁö°Å³ª ´Ù¸¥ ÂÊÄÄÇ»ÅÍ°¡ ¼ÒÄÏÀ» ´ÝÁö ¾ÊÀ» ¶§ ±îÁö ºí·ÏÈ­µÈ´Ù.(¹öÆÛ°¡ Âû¶§±îÁö ±â´Ù¸®°ÚÁö) ¶Ç ´Ù¸¥ ¹®Á¦´Â ÀÌ ÄÚµå´Â ³Ê¹« ¸¹Àº °¡ºñÁö¿Í LineNumberReadr´Â ¼ÒÄÏÀ¸·Î ÀÐÀº µ¥ÀÌÅ͸¦ À§ÇÑ ¹öÆÛ¿Í °°Àº µ¥ÀÌÅ͸¦ À§ÇØ StringÀ» ¸¸µç ´Ù´Â °ÍÀÌ´Ù..¹öÆÛ´Â ´Ù½Ã »ç¿ëµÇ±ä ÇÏÁö¸¸, ½ºÆ®¸µÀº ´Ù½Ã °¡ºñÁö°¡ »¡¸® µÇ´Ï±î Å« ¹®Á¦¸¦ ÀÏÀ¸Å³¼ö ÀÖ´Ù. Write¸Þ¼Òµåµµ ¸¶Âù°¡Áö´Ù. ±æ¾îÁ³´Âµ¥ blocking°ú garbageÀÇ ¹®Á¦°¡ ÀÖ´Ù´Â °ÍÀÌ´Ù. ¿©±â¿¡ thread À̽´±îÁö »ý°¢Çغ¸¸é,JDK1.4ÀÇ ³×Æ®¿öÅ©°ú I/OºÎºÐ¿¡¼­ °¡Àå Å©°Ô ÁÖ¸ñÇØ¾ß ÇÒ °ÍÀÌ non-blockingÀÇ °ø½ÄÀû Áö¿øÀ̶õ »ý°¢ÀÌ µç´Ù. (³Ê¹« Àß ¾Ë·ÁÁø ºÎºÐÀÌ°ÚÁö¸¸, ºñ±³Çϱâ À§ÇØ ÀÏÁ¤ ¸éÀ» Àå½ÄÇß´Ù..) ¢ºNonblocking channel »ý¼ºÇϱ⠱⺻ÀûÀÎ nonblocking¹æ½ÄÀÇ ¼ÒÄÏÀб⠾²±â ÀÛ¾÷À» ±¸ÇöÇϱâ À§ÇØ ½ÇÁ¦ Àб⠾²±â ÀÛ¾÷À» ¼öÇàÇÏ´Â SocketChannelŬ·¡½º¸¦ ´Ù·ç¾î¾ß ÇÑ´Ù. ´ÙÀ½ ÄÚµå´Â ±âÃÊÀûÀÎ ¼­¹ö¼ÒÄÏÇÁ·Î±×·¥À» °³¹ßÇϱâ À§ÇØ º¯°æµÈ nonblocking¹æ½ÄÀ» º¸¿©ÁØ´Ù. ¾Õ¿¡¼­ »ç¿ëµÈ ÄÚµå¿ÍÀÇ Â÷À̸¦ º¸±â ¹Ù¶õ´Ù.

class AcceptThread extends Thread {
  private ServerSocketChannel ssc;
public AcceptThread(Selector connectSelector, ConnectionList list, int port) throws Exception {
    super("Acceptor");
¡¦
   ssc = ServerSocketChannel.open();
//¸ðµç Ưº°ÇÑ ChannelÀÇ °æ¿ì¿Í °°ÀÌ  Á÷Á¢ ServerSocketChannel °´Ã¼¸¦ Á÷Á¢ »ý¼ºÇÒ ¼ö ¾ø°í .
//ServerSocketChannel.open() ÀÇ factory ¸Þ¼Òµå¸¦ »ç¿ëÇØ ¿¬°áµÇÁö ¾ÊÀº SocketChannel»ý¼º
// ÀÌÀü±îÁø java.net.SocketÀ» ¸®ÅÏ¹Þ¾Æ »ç¿ëÇßÁö¸¸,¿©±â¼­´Â ServerSocketÀ» ¸®ÅÏÇÑ´Ù.
      ssc.configureBlocking(false);

    InetSocketAddress address = new InetSocketAddress(port);
    ssc.socket().bind(address);
}
nonblocking¸ðµå¿¡¼­ÀÇ Ã¤³Î»ç¿ë¿¡ ÃÊÁ¡À» ¸ÂÃß°í ÀÖÁö¸¸, ä³ÎÀº blocking°ú nonblocking ¾çÂÊ¿¡¼­ »ç¿ëµÉ ¼ö ÀÖ´Ù. Nonblocking¸ðµå¿¡¼­ ½º·¹µå´Â µ¥ÀÌÅÍ ¾çÀÌ ¾ó¸¶³ª µÇµç Àо°¥ °ÍÀÌ°í °á±¹ ´Ù¸¥ ÀÛ¾÷À» ¼öÇàÇϱâ À§Çؼ­ º¹±ÍÇÒ °ÍÀÌ´Ù. configureBlocking()°¡ ÂüÀ¸·Î Àü´ÞµÇ¸é, ä³ÎÀº Socket¿¡ ´ëÇØ blocking ¸ðµå¿¡¼­ÀÇ Àб⠾²±â ÀÛ¾÷°ú Á¤È®ÇÑ µ¿ÀÏÇÑ ÀÛ¾÷À» ¼öÇàÇÏ°Ô µÉ °ÍÀÌ´Ù. Channel¸¸À¸·Î´Â nonblockingÀÔÃâ·ÂÀ» ±¸ÇöÀ» ¸¸µå´Âµ¥ ÃæºÐÇÏÁö ¾Ê´Ù. ChannelŬ·¡½º´Â nonblockingÀÔÃâ·ÂÀ» À§ÇØ selector Ŭ·¡½º¿Í Çù·ÂÇÏ¿© ÀÛ¾÷ÇؾßÇÑ´Ù. ÀÌ´Â ´ÙÀ½ part¿¡¼­ ¾Ë¾Æº¸±â·Î ÇÏÀÚ. ====== <Âü°íÇϼ¼¿ä> ============ accept()¸¦ ÅëÇØ ¿¬°áµÈ socketChannelÀ» ¾òÀ» ¼öµµ Àִµ¥,blocking mode¿¡¼­ , ServerSocketChannel¿¡¼­ÀÇ accept()¸Þ¼Òµå´Â ¿äû¿¬°áÀÌ µµÂøÇÒ ¶§ ±îÁö ¸®ÅϵÇÁö ¾Ê°í TimeoutExceptionÀÌ ³­´Ù. Non-blocking¸ðµå¿¡¼­´Â accept()´Â Ç×»ó ÁïÈ÷ socketchannelÀ̳ª nullÀ» ¸®ÅÏÇÑ´Ù. http://www.javaworld.com/javaworld/jw-09-2001/jw-0907-merlin_p.html Á» ´õ Àß Á¤¸®µÈ ³»¿ë°ú nonblocking¼­¹ö¼Ò½º¸¦ º¸½Ç ¼ö ÀÖ½À´Ï´Ù.
 
1
References
 
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD