Fundamentals 16 min read

Understanding I/O Models: Synchronous, Asynchronous, Blocking, Non‑Blocking, and High‑Performance Design Patterns

This article explains the fundamental concepts of I/O models—including synchronous vs. asynchronous, blocking vs. non‑blocking, the five classic I/O models, and the Reactor and Proactor design patterns—using clear definitions, examples, and Java code snippets to illustrate each approach.

Java Captain
Java Captain
Java Captain
Understanding I/O Models: Synchronous, Asynchronous, Blocking, Non‑Blocking, and High‑Performance Design Patterns

Many learners find Java NIO challenging, so before diving into NIO programming we first discuss basic I/O concepts: the differences between synchronous and asynchronous, blocking and non‑blocking, and then introduce five I/O models and two high‑performance design patterns (Reactor and Proactor).

1. What are synchronous and asynchronous?

Synchronous execution means tasks must run one after another, causing the whole process to wait while a task runs; asynchronous execution allows tasks to run concurrently without making the whole process wait.

Example code shows a typical synchronous call where fun1() must finish before fun2() runs, and an asynchronous version where each function runs in its own thread.

void fun1() {
    // ...
}

void fun2() {
    // ...
}

void function() {
    fun1();
    fun2();
    // ...
}
void fun1() {
    // ...
}

void fun2() {
    // ...
}

void function() {
    new Thread(){
        public void run() {
            fun1();
        }
    }.start();
    new Thread(){
        public void run() {
            fun2();
        }
    }.start();
    // ...
}

2. What are blocking and non‑blocking?

Blocking: when a request cannot be satisfied, the operation waits until the condition is met. Non‑blocking: the request returns immediately with a status indicating the condition is not yet satisfied.

3. Blocking I/O vs. Non‑blocking I/O

Both involve two stages: checking data readiness and copying data. In blocking I/O the first stage waits; in non‑blocking I/O it returns a flag if data is not ready.

4. Synchronous I/O vs. Asynchronous I/O

Synchronous I/O blocks the requesting thread until the operation completes; asynchronous I/O lets the kernel handle both stages and notifies the thread when finished.

5. Five I/O models

1) Blocking I/O – the thread waits on read() until data is ready.

data = socket.read();

2) Non‑blocking I/O – the thread repeatedly polls the socket and processes data when it becomes available.

while(true){
    data = socket.read();
    if(data != error){
        // process data
        break;
    }
}

3) Multiplexed I/O – a single thread monitors many sockets (Java NIO’s selector) and processes events only when they occur.

4) Signal‑driven I/O – the kernel sends a signal to the thread when data is ready, and the thread’s signal handler performs the I/O.

5) Asynchronous I/O – the kernel completes the I/O and notifies the thread; no thread‑level blocking occurs.

6. Two high‑performance I/O design patterns

Traditional designs use either a thread per connection or a thread pool. Both have scalability limits. The Reactor pattern registers interest in events and uses a single (or few) thread(s) to dispatch events, while the Proactor pattern lets the kernel perform the I/O and notifies the application upon completion.

Reactor corresponds to the multiplexed I/O model; Proactor corresponds to true asynchronous I/O.

Original source: http://www.cnblogs.com/dolphin0520/p/3916526.html

JavaasynchronousNIOI/OReactorblockingProactor
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.