[Basic Concepts of Nio]dawn

Source: Internet
Author: User

1.dawn is single-threaded:

Why single thread? In reality, many programs are single-threaded, such as REDIS,MEMCACHE,NODEJS,MMORPG servers ... There are two advantages to using single threading, first of all, there is no need to use locks, logic is easier to understand, less error prone, and secondly, it reduces the scheduling cost when switching threads.


2. What is a task

Dawn uses a task, which is actually the kilim in the process, the most basic function of the association is suspend and restore, which is consistent with the thread. All tasks are run in the same thread, so there is no need for synchronization between tasks from the perspective of memory visibility and consistency. The exception to this is that Io, if multiple tasks read and write to the same IO device (such as a socket), is potentially unsafe, because when the data is large, it may take more than one read write, at which point the data of multiple tasks is mixed together, causing the receiver to receive chaotic data. Therefore, the IO operation must be atomic, Dawn has taken this into account, and the IO operation in the underlying package ensures the atomicity of the IO operation.


The general use of task, such as doing timed tasks, start a task,sleep for a certain time, wake up, execute a piece of code, and then sleep, loop ....

Again, such as server-side network connection processing, after receiving a connection, you can start a separate task to deal with this connection, packet, unpack, process, packet, send, a loop, and use a separate thread to handle a single connection.

Then you may have doubts, the number of connections, the task is not too much? It doesn't matter, and using threads different, we can start a lot of task, on my machine, using the JVM's default configuration, can start 100,000 task,task between each second can be 200多万次 switch. With so many tasks, most scenes can be dealt with.


3. What is Nonpausabletask

Literally, it is not a task to suspend, indeed, this is a timed task, can not suspend, wake up, can only be executed once. Nonpausabletask because it does not involve suspending, recovering, so more efficient, if only from the point of view of task switching, nonpausabletask switching efficiency is about twice times the task. If you use Nonpausabletask a lot, it is tantamount to a traditional callback-type asynchronous programming, the complexity of the program will increase, if it is a simple task of high frequency execution, you can consider replacing the task with Nonpausabletask, may improve the system throughput. But in general, we should prioritize the program's architecture, logic, and finally consider Nonpausabletask.


4. What is Scheduler?

Just as the thread is dispatched by the kernel, the task is dispatched by scheduler. For example, in a task to perform sleep (2000), is to tell the scheduler, I have to suspend, two seconds after you wake me up, at this time, scheduler will be in the internal to do a 2-second timing, two seconds after the execution of the task resumed.

In addition, scheduler also includes the detection of IO events and the dispatch of events between threads (in development). For example, when a task is waiting for data to arrive on Tcpchannel.read, scheduler detects that the socket is in a readable state, it wakes up the task and lets the task continue execution to read the data.

Each scheduler is a separate thread, so only tasks running on the same scheduler can safely share memory, sockets and other resources.

So, Dawn actually can be extended to multi-threaded, this part of the function is still in the planning, but the thread pool related features will be available soon.

[Basic Concepts of Nio]dawn

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.