程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java中channel用法總結

Java中channel用法總結

編輯:關於JAVA

Java中channel用法總結。本站提示廣大學習愛好者:(Java中channel用法總結)文章只能為提供參考,不一定能成為您想要的結果。以下是Java中channel用法總結正文


本文實例總結了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相干,而不是channel。可以對過程有用,而不是線程。可以經由過程內存映照文件(memory-mapped file)來完成線程同步

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

6.MappedByteBuffer are direct. load( )將全部文件加載到內存(改辦法不克不及包管完成)。force( )將數據flush到硬盤。

7.未綁定端口的DatagramChannel體系會主動分派端口。DatagramChannel的connect(),將包管只接收指定源地址的數據包。這時候候,可使用通俗的read和write辦法,包含Scatter/Gather

願望本文所述對年夜家的java法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved