主頁 > 後端開發 > 華為二面!!!面試官直接問我Java中到底什么是NIO?這不是直接送分題???

華為二面!!!面試官直接問我Java中到底什么是NIO?這不是直接送分題???

2021-10-10 08:16:47 後端開發

華為二面!!!面試官直接問我Java中到底什么是NIO?這不是直接送分題???

  • 什么是NIO
  • 緩沖區(Buffer)
    • 緩沖區型別
    • 獲取緩沖區
    • 核心屬性
    • 核心方法
  • 非直接緩沖區和直接緩沖區
    • 非直接緩沖區
    • 直接緩沖區
  • 通道(Channel)
    • Java Channel
      • 獲得通道的方法
        • 物件呼叫getChannel() 方法
        • getChannel()+非直接緩沖區
        • open()+直接緩沖區
        • 通道間直接傳輸
        • 直接緩沖區VS非直接緩沖區
    • 分散和聚集
  • 非阻塞式網路通信
    • 概念
    • 阻塞式網路通信
    • 非阻塞式網路通信
      • 選擇器

什么是NIO

Java NIO(New IO)是從Java 1.4版本開始引入的一個新的IO API,可以替代標準的Java IO API,NIO與原來的IO有同樣的作用和目的,但是使用的方式完全不同,NIO支持面向緩沖區的、基于通道的IO操作,NIO將以更加高效的方式進行檔案的讀寫操作,

IONIO
面向流(Stream Oriented)面向緩沖區(Buffer Oriented)
阻塞IO(Blocking IO)非阻塞IO(NonBlocking IO)
選擇器(Selectors)

底層原理可見:作業系統-檔案IO

緩沖區(Buffer)

緩沖區型別

Buffer 就像一個陣列,可以保存多個相同型別的資料,根據資料型別不同(boolean 除外) ,有以下Buffer 常用子類

  • ByteBuffer
  • CharBuffer
  • ShortBuffer
  • IntBuffer
  • LongBuffer
  • FloatBuffer
  • DoubleBuffer

各種型別的緩沖區中,都有一個對應型別的陣列,如

ByteBuffer

final byte[] hb;                  // Non-null only for heap buffersCopy

IntBuffer

final int[] hb;                  // Non-null only for heap buffers

image

獲取緩沖區

通過allocate方法可以獲取一個對應緩沖區的物件,它是緩沖區類的一個靜態方法

// 獲取一個容量大小為1024位元組的位元組緩沖區
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

核心屬性

緩沖區的父類Buffer中有幾個核心屬性,如下

// Invariants: mark <= position <= limit <= capacity
private int mark = -1;
private int position = 0;
private int limit;
private int capacity;Copy

  • capacity:緩沖區的容量,通過建構式賦予,一旦設定,無法更改
  • limit:緩沖區的界限,位于limit 后的資料不可讀寫,緩沖區的限制不能為負,并且不能大于其容量
  • position:下一個讀寫位置的索引(類似PC),緩沖區的位置不能為負,并且不能大于limit
  • mark:記錄當前position的值,position被改變后,可以通過呼叫reset() 方法恢復到mark的位置,

以上四個屬性必須滿足以下要求

mark <= position <= limit <= capacity

核心方法

put()方法

  • put()方法可以將一個資料放入到緩沖區中,
  • 進行該操作后,postition的值會+1,指向下一個可以放入的位置,capacity = limit ,為緩沖區容量的值,

image

flip()方法

  • flip()方法會切換對緩沖區的操作模式,由寫->讀 / 讀->寫
  • 進行該操作后
    • 如果是寫模式->讀模式,position = 0 , limit 指向最后一個元素的下一個位置,capacity不變
    • 如果是讀->寫,則恢復為put()方法中的值

image

get()方法

  • get()方法會讀取緩沖區中的一個值
  • 進行該操作后,position會+1,如果超過了limit則會拋出例外

rewind()方法

  • 該方法只能在讀模式下使用
  • rewind()方法后,會恢復position、limit和capacity的值,變為進行get()前的值

clean()方法

  • clean()方法會將緩沖區中的各個屬性恢復為最初的狀態,position = 0, capacity = limit
  • 此時緩沖區的資料依然存在,處于“被遺忘”狀態,下次進行寫操作時會覆寫這些資料

image

mark()和reset()方法

  • mark()方法會將postion的值保存到mark屬性中
  • reset()方法會將position的值改為mark中保存的值

使用展示

import java.nio.ByteBuffer;

public class demo1 {
    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        System.out.println("放入前引數");
        System.out.println("position " + byteBuffer.position());
        System.out.println("limit " + byteBuffer.limit());
        System.out.println("capacity " + byteBuffer.capacity());
        System.out.println();

        System.out.println("------put()------");
        System.out.println("放入3個資料");
        byte bt = 1;
        byteBuffer.put(bt);
        byteBuffer.put(bt);
        byteBuffer.put(bt);

        System.out.println("放入后引數");
        System.out.println("position " + byteBuffer.position());
        System.out.println("limit " + byteBuffer.limit());
        System.out.println("capacity " + byteBuffer.capacity());
        System.out.println();

        System.out.println("------flip()-get()------");
        System.out.println("讀取一個資料");
        // 切換模式
        byteBuffer.flip();
        byteBuffer.get();

        System.out.println("讀取后引數");
        System.out.println("position " + byteBuffer.position());
        System.out.println("limit " + byteBuffer.limit());
        System.out.println("capacity " + byteBuffer.capacity());
        System.out.println();

        System.out.println("------rewind()------");
        byteBuffer.rewind();
        System.out.println("恢復后引數");
        System.out.println("position " + byteBuffer.position());
        System.out.println("limit " + byteBuffer.limit());
        System.out.println("capacity " + byteBuffer.capacity());
        System.out.println();

        System.out.println("------clear()------");
        // 清慷訓沖區,這里只是恢復了各個屬性的值,但是緩沖區里的資料依然存在
        // 但是下次寫入的時候會覆寫緩沖區中之前的資料
        byteBuffer.clear();
        System.out.println("清空后引數");
        System.out.println("position " + byteBuffer.position());
        System.out.println("limit " + byteBuffer.limit());
        System.out.println("capacity " + byteBuffer.capacity());
        System.out.println();
        System.out.println("清空后獲得資料");
        System.out.println(byteBuffer.get());

    }
}

放入前引數
position 0
limit 1024
capacity 1024

------put()------
放入3個資料
放入后引數
position 3
limit 1024
capacity 1024

------flip()-get()------
讀取一個資料
讀取后引數
position 1
limit 3
capacity 1024

------rewind()------
恢復后引數
position 0
limit 3
capacity 1024

------clear()------
清空后引數
position 0
limit 1024
capacity 1024

清空后獲得資料
1

Process finished with exit code 0

非直接緩沖區和直接緩沖區

非直接緩沖區

通過allocate()方法獲取的緩沖區都是非直接緩沖區,這些緩沖區是建立在JVM堆記憶體之中的,

public static ByteBuffer allocate(int capacity) {
   if (capacity < 0)
   throw new IllegalArgumentException();

   // 在堆記憶體中開辟空間
   return new HeapByteBuffer(capacity, capacity);
}

HeapByteBuffer(int cap, int lim) {        // package-private
   // new byte[cap] 創建陣列,在堆記憶體中開辟空間
   super(-1, 0, lim, cap, new byte[cap], 0);
   /*
   hb = new byte[cap];
   offset = 0;
   */
}

通過非直接緩沖區,想要將資料寫入到物理磁盤中,或者是從物理磁盤讀取資料,都需要經過JVM和作業系統,資料在兩個地址空間中傳輸時,會copy一份保存在對方的空間中,所以費直接緩沖區的讀取效率較低.,

image

直接緩沖區

只有ByteBuffer可以獲得直接緩沖區,通過allocateDirect()獲取的緩沖區為直接緩沖區,這些緩沖區是建立在物理記憶體之中的,

public static ByteBuffer allocateDirect(int capacity) {
   return new DirectByteBuffer(capacity);
}

DirectByteBuffer(int cap) {                   // package-private
   ...
   // 申請物理記憶體
   boolean pa = VM.isDirectMemoryPageAligned();
   ...
}

直接緩沖區通過在作業系統和JVM之間創建物理記憶體映射檔案加快緩沖區資料讀/寫入物理磁盤的速度,放到物理記憶體映射檔案中的資料就不歸應用程式控制了,作業系統會自動將物理記憶體映射檔案中的資料寫入到物理記憶體中,

image

通道(Channel)

Channel由java.nio.channels 包定義的,Channel 表示IO 源與目標打開的連接,Channel 類似于傳統的“流”,只不過Channel 本身不能直接訪問資料,Channel 只能與Buffer 進行互動

應用程式進行讀寫操作呼叫函式時,底層呼叫的作業系統提供給用戶的讀寫API,呼叫這些API時會生成對應的指令,CPU則會執行這些指令,在計算機剛出現的那段時間,所有讀寫請求的指令都有CPU去執行,過多的讀寫請求會導致CPU無法去執行其他命令,從而CPU的利用率降低,

image

后來,DMA(Direct Memory Access,直接存盤器訪問)出現了,當IO請求傳到計算機底層時,DMA會向CPU請求,讓DMA去處理這些IO操作,從而可以讓CPU去執行其他指令,DMA處理IO操作時,會請求獲取總線的使用權,當IO請求過多時,會導致大量總線用于處理IO請求,從而降低效率

image

于是便有了Channel(通道),Channel相當于一個專門用于IO操作的獨立處理器,它具有獨立處理IO請求的能力,當有IO請求時,它會自行處理這些IO請求 ,

image

Java Channel

image

  • 本地檔案IO
    • FileChannel
  • 網路IO
    • SocketChanel、ServerSocketChannel:用于TCP傳輸
    • DatagramChannel:用于UDP傳輸

獲得通道的方法

物件呼叫getChannel() 方法

獲取通道的一種方式是對支持通道的物件呼叫getChannel() 方法,支持通道的類如下:

  • FileInputStream
  • FileOutputStream
  • RandomAccessFile
  • DatagramSocket
  • Socket
  • ServerSocket

例子:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.DatagramChannel;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;

public class demo2 {
    public static void main(String[] args) throws IOException {
        // 本地通道
        FileInputStream fileInputStream = new FileInputStream("zwt");
        FileChannel channel1 = fileInputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream("zwt");
        FileChannel channel2 = fileOutputStream.getChannel();

        // 網路通道
        Socket socket = new Socket();
        SocketChannel channel3 = socket.getChannel();

        ServerSocket serverSocket = new ServerSocket();
        ServerSocketChannel channel4 = serverSocket.getChannel();

        DatagramSocket datagramSocket = new DatagramSocket();
        DatagramChannel channel5 = datagramSocket.getChannel();

        // 最后要關閉通道

        FileChannel open = FileChannel.open(Paths.get("zwt"));

        SocketChannel open1 = SocketChannel.open();

    }
}

getChannel()+非直接緩沖區

  • getChannel()獲得通道
  • allocate()獲得非直接緩沖區

通過非直接緩沖區讀寫資料,需要通過通道來傳輸緩沖區里的資料

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class demo4 {
    public static void main(String[] args) {
        FileInputStream is = null;
        FileOutputStream os = null;
        // 獲得通道
        FileChannel inChannel = null;
        FileChannel outChannel = null;

        // 利用 try-catch-finally 保證關閉
        try {
            is = new FileInputStream("");
            os = new FileOutputStream("");

            // 獲得通道
            inChannel = is.getChannel();
            outChannel = os.getChannel();

            // 獲得緩沖區,用于在通道中傳輸資料
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

            // 回圈將位元組資料放入到buffer中,然后寫入磁盤中
            while (inChannel.read(byteBuffer) != -1) {
                // 切換模式
                byteBuffer.flip();
                outChannel.write(byteBuffer);
                byteBuffer.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inChannel != null) {
                try {
                    inChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outChannel != null) {
                try {
                    outChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

open()+直接緩沖區

  • 通過open獲得通道
  • 通過FileChannel.map()獲取直接緩沖區

使用直接緩沖區時,無需通過通道來傳輸資料,直接將資料放在緩沖區內即可

import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class demo5 {
   public static void main(String[] args) throws IOException {
       // 通過open()方法來獲得通道
       FileChannel inChannel = FileChannel.open(Paths.get(""), StandardOpenOption.READ);

       // outChannel需要為 READ WRITE CREATE模式
       // READ WRITE是因為后面獲取直接緩沖區時模式為READ_WRITE模式
       // CREATE是因為要創建新的檔案
       FileChannel outChannel = FileChannel.open(Paths.get(""), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);

       // 獲得直接緩沖區
       MappedByteBuffer inMapBuf = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
       MappedByteBuffer outMapBuf = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());

       // 位元組陣列
       byte[] bytes = new byte[inMapBuf.limit()];

       // 因為是直接緩沖區,可以直接將資料放入到記憶體映射檔案,無需通過通道傳輸
       inMapBuf.get(bytes);
       outMapBuf.put(bytes);

       // 關倍訓沖區,這里沒有用try-catch-finally
       inChannel.close();
       outChannel.close();
   }
}

通道間直接傳輸

public static void channelToChannel() throws IOException {
  long start = System.currentTimeMillis();
  // 通過open()方法來獲得通道
  FileChannel inChannel = FileChannel.open(Paths.get(""), StandardOpenOption.READ);

  // outChannel需要為 READ WRITE CREATE模式
  // READ WRITE是因為后面獲取直接緩沖區時模式為READ_WRITE模式
  // CREATE是因為要創建新的檔案
  FileChannel outChannel = FileChannel.open(Paths.get(""), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);

  // 通道間直接傳輸
  inChannel.transferTo(0, inChannel.size(), outChannel);
  // 對應的還有transferFrom
  // outChannel.transferFrom(inChannel, 0, inChannel.size());

  inChannel.close();
  outChannel.close();
}

直接緩沖區VS非直接緩沖區

// getChannel() + 非直接緩沖區耗時
708
// open() + 直接緩沖區耗時
115
// channel transferTo channel耗時
47

直接緩沖區的讀寫速度雖然很快,但是會占用很多很多記憶體空間,如果檔案過大,會使得計算機運行速度變慢

分散和聚集

分散讀取

分散讀取(Scattering Reads)是指從Channel 中讀取的資料“分散”到多個Buffer 中,

注意:按斬訓沖區的順序,從Channel 中讀取的資料依次將 Buffer 填滿,

聚集寫入

聚集寫入(Gathering Writes)是指將多個Buffer 中的資料“聚集”到Channel,

按斬訓沖區的順序,寫入position 和limit 之間的資料到Channel,

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class demo6 {
    public static void main(String[] args) throws IOException {
        FileInputStream is = new FileInputStream("");
        FileOutputStream os = new FileOutputStream("");

        FileChannel inChannel = is.getChannel();
        FileChannel outChannel = os.getChannel();

        // 獲得多個緩沖區,并且放入到緩沖區陣列中
        ByteBuffer byteBuffer1 = ByteBuffer.allocate(50);
        ByteBuffer byteBuffer2 = ByteBuffer.allocate(1024);
        ByteBuffer[] byteBuffers = {byteBuffer1, byteBuffer2};

        // 分散讀取
        inChannel.read(byteBuffers);

        byteBuffer1.flip();
        byteBuffer2.flip();

        // 聚集寫入
        outChannel.write(byteBuffers);
    }
}

非阻塞式網路通信

概念

底層原理可見:作業系統-檔案IO

比喻:

舉個你去飯堂吃飯的例?,你好??戶程式,飯堂好?作業系統,

阻塞 I/O 好?,
你去飯堂吃飯,但是飯堂的菜還沒做好,然后你就?直在那?等啊等,

等了好??段時間終于等到飯堂阿姨把菜端了出來(資料準備的程序),

但是你還得繼續等阿姨把菜(內核空間)打到你的飯盒?(?戶空間),

經歷完這兩個程序,你才可以離開,

?阻塞 I/O 好?,
你去了飯堂,問阿姨菜做好了沒有,阿姨告訴你沒,

你就離開了,過??分鐘,你?來,

飯堂問阿姨,阿姨說做好了,于是阿姨幫你把菜打到你的飯盒?,這個程序你是得等待的,

基于?阻塞的 I/O 多路復?好?,
你去飯堂吃飯,發現有?排窗?,飯堂阿姨告訴你這些窗?都還沒做好菜,

等做好了再通知你,于是等啊等( select 調?中),過了?會阿姨通知你菜做好了,

但是不知道哪個窗?的菜做好了,你??看吧,

于是你只能?個?個窗?去確認,后?發現 5 號窗?菜做好了,

于是你讓 5 號窗?的阿姨幫你打菜到飯盒?,這個打菜的程序你是要等待的,雖然時間不?,

打完菜后,你?然就可以離開了,

異步 I/O 好?,
你讓飯堂阿姨將菜做好并把菜打到飯盒?后,把飯盒送到你?前,整個程序你都不需要任何等待,

阻塞式網路通信

package NIOAndBIO;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class BIO {
    public static void main(String[] args) throws IOException {
        Thread thread1 = new Thread(() -> {
            try {
                server();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        Thread thread2 = new Thread(() -> {
            try {
                client();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        thread1.start();
        thread2.start();
    }

    public static void client() throws IOException {
        // 創建客戶端通道
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 2022));

        // 讀取資訊 D:\\bizhi\\bizhi202008\\wallhaven-kwp2qq.jpg
        FileChannel fileChannel = FileChannel.open(Paths.get("D:\\\\bizhi\\\\bizhi202008\\\\wallhaven-kwp2qq.jpg"), StandardOpenOption.READ);

        // 創建緩沖區
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        // 寫入資料
        while (fileChannel.read(byteBuffer) != -1) {
            byteBuffer.flip();
            socketChannel.write(byteBuffer);
            byteBuffer.clear();
        }

        fileChannel.close();
        socketChannel.close();
    }

    public static void server() throws IOException {
        // 創建服務端通道
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

        FileChannel fileChannel = FileChannel.open(Paths.get("D:\\\\bizhi\\\\bizhi202008\\\\wallhaven-kwp2qq.jpg"), StandardOpenOption.WRITE, StandardOpenOption.CREATE);

        // 系結鏈接
        serverSocketChannel.bind(new InetSocketAddress(2022));

        // 獲取客戶端的通道
        SocketChannel socketChannel = serverSocketChannel.accept();

        // 創建緩沖區
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        while (socketChannel.read(byteBuffer) != -1) {
            byteBuffer.flip();
            fileChannel.write(byteBuffer);
            byteBuffer.clear();
        }

        socketChannel.close();
        fileChannel.close();
        serverSocketChannel.close();
    }
}

非阻塞式網路通信

package NIOAndBIO;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;

public class NIO {
    public static void main(String[] args) {
        Thread thread1 = new Thread(()->{
            try {
                server();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        Thread thread2 = new Thread(()->{
            try {
                client();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        thread1.start();
        thread2.start();
    }

    public static void client() throws IOException {
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 2020));

        // 設定為非阻塞模式
        socketChannel.configureBlocking(false);

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String str = scanner.next();
            byteBuffer.put(str.getBytes());
            byteBuffer.flip();
            socketChannel.write(byteBuffer);
            byteBuffer.clear();
        }

        byteBuffer.clear();

        socketChannel.close();
    }

    public static void server() throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.bind(new InetSocketAddress(2020));

        // 獲得選擇器
        Selector selector = Selector.open();

        // 將通道注冊到選擇器中,設定為接收操作
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        // 輪詢接受
        while (selector.select() > 0) {
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            // 獲得事件的key
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                if (key.isAcceptable()) {
                    SocketChannel socketChannel = serverSocketChannel.accept();
                    socketChannel.configureBlocking(false);
                    socketChannel.register(selector, SelectionKey.OP_READ);
                } else if (key.isReadable()) {
                    // 從選擇器中獲取通道
                    SocketChannel socketChannel = (SocketChannel) key.channel();

                    ByteBuffer byteBuffer = ByteBuffer.allocate(10);

                    while (socketChannel.read(byteBuffer) != -1) {
                        int len = byteBuffer.limit();
                        byteBuffer.flip();
                        System.out.println(new String(byteBuffer.array(), 0, len));
                        byteBuffer.clear();
                    }
                    socketChannel.close();
                }
                iterator.remove();
            }
        }
        serverSocketChannel.close();
    }
}

選擇器

選擇器(Selector)是SelectableChannle 物件的多路復用器,Selector 可以同時監控多個SelectableChannel 的IO 狀況,也就是說,利用Selector 可使一個單獨的執行緒管理多個Channel,Selector 是非阻塞IO 的核心

image

選擇器的創建

// 創建一個選擇器
Selector selector = Selector.open();

系結選擇器

通過呼叫通道的register方法可以系結選擇器,register方法有兩個引數

  • Selector:即系結哪個選擇器
  • ops:監聽事件型別,ops有4個值可以選擇,為SelectionKey的靜態屬性
// 讓選擇器監聽一種狀態
myChannel.register(selector, SelectionKey.OP_READ);
// 讓選擇器監聽多種狀態
myChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_ACCEPT);

SelectionKey

表示SelectableChannel 和Selector 之間的注冊關系,每次向選擇器注冊通道時就會選擇一個事件(選擇鍵),選擇鍵包含兩個表示為整數值的操作集,操作集的每一位都表示該鍵的通道所支持的一類可選擇操作,

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/307234.html

標籤:java

上一篇:面試官問我List介面,我

下一篇:這篇博客和你嘮嘮 python 并發,滾雪球學python第四季,第16篇

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more