最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中channel用法總結(jié)

 更新時(shí)間:2015年06月08日 16:07:46   作者:liuzx32  
這篇文章主要介紹了Java中channel用法,較為詳細(xì)的總結(jié)了channel的定義、類型及使用技巧,需要的朋友可以參考下

本文實(shí)例總結(jié)了Java中channel用法。分享給大家供大家參考。具體分析如下:

1.Channel接口的定義:

public interface Channel
{
  public boolean isOpen( );
  public void close( ) throws IOException;
}

2.Channel的常見類型:

FileChannel, SocketChannel, ServerSocketChannel, and DatagramChannel;
FileChannel通過RandomAccessFile, FileInputStream, FileOutputStream的getChannel()來初始化。

SocketChannel sc = SocketChannel.open();
sc.connect (new InetSocketAddress ("somehost", someport));
ServerSocketChannel ssc = ServerSocketChannel.open( );
ssc.socket().bind (new InetSocketAddress (somelocalport));
DatagramChannel dc = DatagramChannel.open();

3.Scatter/Gather,必須使用ByteBuffer.allocateDirect(100)

public interface ScatteringByteChannel extends ReadableByteChannel {
  public long read (ByteBuffer [] dsts) throws IOException;
  public long read (ByteBuffer [] dsts, int offset, int length) throws IOException;
}
public interface GatheringByteChannel extends WritableByteChannel {
  public long write(ByteBuffer[] srcs) throws IOException;
  public long write(ByteBuffer[] srcs, int offset, int length) throws IOException;
}

4.file lock是和file相關(guān),而不是channel??梢詫M(jìn)程有效,而不是線程。可以通過內(nèi)存映射文件(memory-mapped file)來實(shí)現(xiàn)線程同步

5.buffer = fileChannel.map (FileChannel.MapMode.READ_ONLY, 100, 200);

6.MappedByteBuffer are direct. load( )將整個(gè)文件加載到內(nèi)存(改方法不能保證完成)。force( )將數(shù)據(jù)flush到硬盤。

7.未綁定端口的DatagramChannel系統(tǒng)會(huì)自動(dòng)分配端口。DatagramChannel的connect(),將保證只接受指定源地址的數(shù)據(jù)包。這時(shí)候,可以使用普通的read和write方法,包括Scatter/Gather

希望本文所述對大家的java程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論

贵州省| 新巴尔虎右旗| 苍溪县| 视频| 礼泉县| 抚远县| 若尔盖县| 民乐县| 舟山市| 北安市| 天气| 德昌县| 陵水| 金山区| 延安市| 嵩明县| 剑河县| 惠来县| 宁都县| 揭阳市| 遂昌县| 黄龙县| 岑溪市| 嘉禾县| 丰城市| 巍山| 武胜县| 房山区| 沁阳市| 麦盖提县| 专栏| 钟山县| 濮阳市| 巨鹿县| 会同县| 扶绥县| 阿拉善右旗| 义马市| 洛浦县| 洛川县| 巴彦淖尔市|