"Java TCP/IP socket" Java NIO socket VS standard IO socket

Source: Internet
Author: User

Introduction

Java NiO, introduced from JDK1.4, provides a completely different way of working with standard IO.

The NIO package (java.nio.*) introduces four key abstract data types that work together to address some of the problems in traditional I/O classes.
    1, Buffer: It is a linear table structure that contains data and is used for reading and writing. It also provides a special class for I/O operations for memory-mapped files.
    2, Charset: It provides the operation of a Unicode string that is mapped to a sequence of bytes and inverse innuendo.
    3, Channels: Contains three pipes of socket,file and pipe, it is actually a two-way communication channel.
    4, Selector: It centralizes multiple asynchronous I/O operations into one or more threads.

Compare

 

    1, read and write Data

    Standard IO is based on byte stream and character stream, it can not move data in the stream, and NiO is based on the channel (channels) and buffers (buffer), the data is always read from the channel into the buffer, or from the buffer write to the channel, You can move the saved data back and forth in the buffer when needed.

 

    2, non-blocking

   

    Blocking Network  IO  features:

    1) multi-threaded processing of multiple connections. Each thread has its own stack space and takes up some  CPU  time.

    2) Each thread is blocked when it is not ready to be externally. The result of blocking is a lot of context switching. And most of the context switches may be meaningless. For example, if a thread listens on a port, only a few requests come in a day, but the  cpu  has to do a context-switching attempt for that thread, and most of the switching ends up blocking.

    NIO has non-blocking characteristics and can realize non-blocking channel by configuring the blocking behavior of channel. In non-blocking situations, threads can do other things while waiting for connections, writing data, etc. (blocking operations in standard IO), which enables the asynchronous operation of threads.

    Non-blocking network IO features:

    1) the entire process into small tasks, through the collaboration between tasks to complete.

    2) consists of a dedicated thread that handles all  IO  events and is responsible for distribution.

    3) event-driven: events are triggered at the time of event, rather than synchronized to monitor events.

    4) thread communication: between threads through  wait,notify  etc way communication. Ensure that each context switch is meaningful. Reduce the unnecessary process switching.

     3, selector

Java NiO introduces the concept of selectors, where selectors can listen to events from multiple channels (e.g., connection open, data arrival). Therefore, a single thread can listen to multiple data channels, which is also the core of non-blocking IO. In socket programming for standard IO, a single thread can only listen on one port.

"Java TCP/IP socket" Java NIO socket VS standard IO socket

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.