Understanding Synchronous, Asynchronous, and Callback Calls in Java

The article explains three module invocation methods—synchronous, asynchronous, and callback—detailing their mechanisms, advantages, and trade‑offs, and provides a complete Java example where a teacher asks a student a question and receives the answer via a callback interface, illustrating both synchronous and asynchronous scenarios.

ITPUB
ITPUB
ITPUB
Understanding Synchronous, Asynchronous, and Callback Calls in Java

Module Invocation Types

In any application system, regardless of programming language, modules need to call each other. The article classifies these calls into three types: synchronous, asynchronous, and callback.

Synchronous Call

A synchronous call is the most basic and straightforward method: method a() in class A invokes method b() in class B and waits until b() finishes before continuing. It works well when b() executes quickly; otherwise the whole flow can become blocked.

Asynchronous Call

An asynchronous call solves the blocking problem by starting a new thread for the callee. Class A’s method a() launches a thread that runs class B’s method b() and then proceeds immediately. If a() needs the result of b(), it must listen for a callback. In Java this is often done with Future + Callable.

Callback

A callback creates a two‑way interaction:

Class A’s method a() calls class B’s method b().

When b() finishes, it actively invokes a callback method defined in class A.

This pattern forms a bidirectional call relationship.

Code Example

The following Java example models a teacher asking a student a question; the student thinks for a while and then reports the answer back via a callback.

Define a callback interface:

public interface Callback {
    void tellAnswer(int answer);
}

Teacher class implements the callback:

public class Teacher implements Callback {
    private Student student;
    public Teacher(Student student) { this.student = student; }
    public void askQuestion() { student.resolveQuestion(this); }
    @Override
    public void tellAnswer(int answer) {
        System.out.println("知道了,你的答案是" + answer);
    }
}

Student interface:

public interface Student {
    void resolveQuestion(Callback callback);
}

Concrete student implementation (Ricky):

public class Ricky implements Student {
    @Override
    public void resolveQuestion(Callback callback) {
        // Simulate solving the problem
        try { Thread.sleep(3000); } catch (InterruptedException e) {}
        // Callback with the answer
        callback.tellAnswer(3);
    }
}

Test class to run the scenario:

public class CallbackTest {
    @Test
    public void testCallback() {
        Student student = new Ricky();
        Teacher teacher = new Teacher(student);
        teacher.askQuestion();
    }
}

Running the test prints a single line:

知道了,你的答案是3

Analysis

The flow is simple: the teacher calls resolveQuestion on the student, the student processes the request, and then invokes tellAnswer on the teacher. This establishes a bidirectional relationship.

Two levels of abstraction are highlighted:

Abstracting the teacher : The student only knows it must call back via the Callback interface, so the concrete teacher can be swapped without affecting the student.

Abstracting the student : The teacher can work with a single Student reference or a collection (e.g., List<Student>) to ask multiple students in a loop, increasing flexibility.

The example demonstrates why callbacks are useful: they allow separation of concerns, easy extension of returned data, and support for asynchronous scenarios where multiple results may arrive at different times.

Regarding synchronous vs. asynchronous callbacks, a simple return value may suffice for a single piece of data, but when multiple pieces of information or timing differences matter, callbacks provide a cleaner, more extensible solution. The article also notes that asynchronous callbacks enable parallel questioning of many students, with each student reporting its answer as soon as it is ready.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Design PatternsJavaAsynchronouscallbackSynchronousModule Interaction
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

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.