《Java Nio》的原文摘录

  • Buffers are not thread-safe. If you want to access a given buffer concurrently from multiple threads, you'll need to do your own synchronization (e.g., acquiring a lock on the buffer object) prior to accessing the buffer. (查看原文)
    stephansun 2013-08-14 11:24:37
    —— 引自第32页
  • The effect of calling compact() is to drop the data elements already drained, preserve what hasn't been drained, and make the buffer ready to resume filling to capacity. You can use a buffer in this way as a First In First Out (FIFO) queue. More efficient algorithms certainly exist (buffer shifting is not a very efficient way to do queuing), but compacting may be a convenient way to synchronize a buffer with logical blocks of data (packets) in a stream you are reading from a socket. (查看原文)
    stephansun 2013-08-14 11:43:46
    —— 引自第35页
  • If a buffer was created as a view of a ByteBuffer object (see Section 2.4.3), then the value returned by the order() method is the byte-order setting of the originating ByteBuffer at the time the view was created. The byte-order setting of the view cannot be changed after it's created and will not be affected if the original byte buffer's byte order is changed later. (查看原文)
    stephansun 2013-08-15 10:32:22
    —— 引自第51页
  • View buffers can potentially be much more efficient when derived from direct byte buffers. If the byte order of the view matches the native hardware byte order, the low-level code may be able to access the data values directly rather than going through the byte-packing and -unpacking process. (查看原文)
    stephansun 2013-08-15 10:49:53
    —— 引自第55页
  • Socket channels are thread-safe. Multiple threads do not need to take special steps to protect against concurrent access, but only one read and one write operation will be in progress at any given time. Keep in mind that sockets are stream-oriented, not packet-oriented. They guarantee that the bytes sent will arrive in the same order but make no promises about maintaining groupings. A sender may write 20 bytes to a socket, and the receiver gets only 3 of those bytes when invoking read(). The remaining 17 bytes may still be in transit. For this reason, it's rarely a good design choice to have multiple, noncooperating threads share the same side of a stream socket. (查看原文)
    stephansun 2013-08-20 10:33:24
    —— 引自第110页
  • * eight-byte buffer is used to send/recieve. But getLong() * eight-byte buffer is used to send/receive. But getLong() (查看原文)
    stephansun 2013-08-20 11:28:04
    —— 引自第116页