Table of Contents
Channel and Buffer
Selector
Home Java javaTutorial Detailed introduction to JAVA-1NIO

Detailed introduction to JAVA-1NIO

Jun 25, 2017 am 10:44 AM
Overview

JAVA-1NIO Overview

Java NIO consists of the following core parts:

  • Channels

  • Buffers

  • Selectors

Although there are many other classes and components in Java NIO, in In my opinion, Channel, Buffer and Selector constitute the core API. Other components, such as Pipe and FileLock, are just utility classes used with the three core components. Therefore, in this overview I will focus on these three components. Other components are covered in separate chapters.

Channel and Buffer

Basically, all IO starts from a Channel in NIO. Channels are a bit like streams. Data can be read from the Channel to the Buffer, or written from the Buffer to the Channel. Here is an illustration:

There are several types of Channel and Buffer. The following are the implementations of some major Channels in JAVA NIO:

  • FileChannel

  • DatagramChannel

  • SocketChannel

  • ServerSocketChannel

As you can see, these channels cover UDP and TCP network IO, as well as file IO.

Along with these classes there are some interesting interfaces, but for the sake of simplicity I tried not to mention them in the overview. I will explain them in other chapters of this tutorial where they are relevant.

The following is the key Buffer implementation in Java NIO:

  • ByteBuffer

  • CharBuffer

  • DoubleBuffer

  • FloatBuffer

  • IntBuffer

  • LongBuffer

  • ShortBuffer

These Buffers cover the basic data types you can send through IO: byte, short, int, long, float, double and char.

Java NIO also has a MappedByteBuffer, which is used to represent memory mapped files. I am not going to explain it in the overview.

Selector

Selector allows a single thread to process multiple Channels. If your application opens multiple connections (channels), but the traffic of each connection is very low, using Selector can be convenient. For example, in a chat server.

This is an illustration of using a Selector to process three Channels in a single thread:

To use the Selector, you must register the Channel with the Selector, and then Call its select() method. This method will block until a registered channel has an event ready. Once this method returns, the thread can handle these events. Examples of events are new connections coming in, data receiving, etc.

The above is the detailed content of Detailed introduction to JAVA-1NIO. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
Overview of operator overloading problems and solutions in C++ Overview of operator overloading problems and solutions in C++ Oct 08, 2023 pm 01:02 PM

Overview of Operator Overloading Problems and Solutions in C++ Introduction: Operator overloading is an important feature of the C++ language, which allows programmers to customize existing operators to operate custom data types. However, operator overloading needs to be used with caution, because if used improperly or excessively, it will lead to problems such as reduced code readability, ambiguity, and reduced efficiency. This article will outline common problems with operator overloading in C++ and provide corresponding solutions and code examples. 1. Problems with operator overloading 1.1 Ambiguity problem in operator overloading

Python Tkinter shows off its power: Create stunning GUI applications Python Tkinter shows off its power: Create stunning GUI applications Mar 24, 2024 am 09:11 AM

Tkinter is a powerful GUI library in python that can be used to create cross-platform desktop applications. With its ease of use and wide range of features, it provides various tools for building user interfaces, handling events and managing layouts. Creating a GUI Window To create a GUI window, you need to use the Tkinter.Tk() method. This method returns a Tk() object that represents the application's main window. A window can have a title using the title() method, and the window size and position using the geometry() method. importtkinterastkroot=tk.Tk()root.title("My first Tkinter application")root.g

PHP High Availability: Best Practices for 24/7 Application Availability PHP High Availability: Best Practices for 24/7 Application Availability Mar 26, 2024 pm 09:31 PM

Achieving high availability of applications is critical to ensure seamless operation of critical business services. For applications built with PHP, there are several best practices that can be used to achieve 24/7 availability. Failover and fault-tolerant load balancing: Use a backend load balancer to distribute traffic to multiple servers to avoid single points of failure. Failover: Configure an automatic failover mechanism to transfer traffic to an alternate server in the event of a failure. Fault-tolerant encoding: Use fault-tolerant encoding techniques, such as RaiD or erasure codes, to protect data from disk failures. Redundant and elastic auto-scaling: Enable auto-scaling to dynamically add or remove servers based on load. Multi-AZ deployment: Deploy applications to multiple Availability Zones (AZ) to maximize

Overview of EventLoop library in PHP8.0 Overview of EventLoop library in PHP8.0 May 14, 2023 am 08:49 AM

With the development of the PHP language, developers need more tools to solve the needs and challenges of modern applications, one of which is event-driven programming, and the EventLoop library of PHP8.0 was born for this purpose. This article will provide an overview and introduction to the library. What is EventLoop In traditional PHP applications, most operations are synchronous. In other words, the program will execute some code, then wait for the relevant data to return, and then continue to execute subsequent code. This programming model is useful for some applications

Introducing Wireless Networks Introducing Wireless Networks Feb 19, 2024 pm 10:38 PM

Overview of Wireless Networks With the rapid development of technology, wireless networks have become an indispensable part of modern life. Our mobile phones, computers, smart homes and other devices all rely on wireless networks for communication and connection. In this article, we will provide an overview of wireless networks and discuss its development, principles, and applications. The development of wireless networks can be traced back to radio communication technology in the 19th century. At that time, people used radio waves to realize long-distance sound and image transmission, pioneering wireless communication. With the further development of electronic technology

Revealing the behind-the-scenes story of the Yii framework: Controlling a new realm of PHP development Revealing the behind-the-scenes story of the Yii framework: Controlling a new realm of PHP development Mar 26, 2024 am 10:31 AM

The Yii framework is a modern, high-performance PHP framework designed to simplify and accelerate WEB application development. It provides a robust foundation that enables developers to focus on business logic rather than low-level details. Behind the Scenes Modular Architecture: Yii adopts a modular architecture so that applications can be easily extended and customized. A module is an independent, reusable block of code that can be used to implement a specific functionality, such as user management or e-commerce. MVC Pattern: Yii follows the mvc (Model-View-Controller) pattern, which separates the application logic from the presentation layer. This promotes code maintainability and improves application testability. ORM support: Yii provides a powerful object-relational mapping (ORM) layer that enables developers to

PHP namespace overview PHP namespace overview Aug 20, 2023 am 11:29 AM

IntroductionInPHP,useofnamespacesallowsclasses/functions/constantsofsamenamebeusedindifferentcontextswithoutanyconflict,therebyencapsulatingtheseitems.Anamespaceislogicalgroupingofclasses/functionsetcdependingontheirrelevence.Justasafilewithsamenamec

An overview of reference types in Go language An overview of reference types in Go language Feb 22, 2024 pm 01:27 PM

Overview of reference types in Go language Go language is an open source programming language developed by Google. One of its design goals is to be concise, efficient, and easy to use. In the Go language, reference types are a special data type that store references to data in memory rather than the data itself. This article will introduce reference types in Go language and provide specific code examples. Reference types include slices, maps, channels, and functions. These types are used in Go

See all articles