Java中的非同步和同步回撥


Java 是一種程式語言和計算平臺,最初由 Sun Microsystems 於 1995 年推出。它從小小的起源發展壯大,如今為當今數字世界的大部分提供了動力,它構成了許多服務和應用程式開發的堅實基礎。Java 仍在為未來開發的尖端產品和數字服務中使用。

什麼是Java中的非同步和同步?

Java 的非同步程式設計正規化使團隊能夠分配工作負載並在主應用程式執行緒之外開發應用程式功能。當團隊準備好該功能時,程式碼隨後與主執行緒同步。

Java 的同步功能允許管理多個執行緒對任何共享資源的訪問。在多執行緒概念中,多個執行緒嘗試同時訪問相同的共享資源,這會導致不可預測的結果。為了確保執行緒之間可靠的通訊,需要同步。

什麼是Java中的回撥?

CallBack 關鍵詞定義了 JAVA 程式設計中與許多其他程式語言中的組織中的函式。CallBack 特性是一個傳遞給另一個函式的函式。CallBack 函式在選定計劃上觸發特定事件後執行。CallBack 特性的實際原因是向特定類告知另一個類的執行狀態。當我們處理某些非同步任務時,CallBack 函式非常有用。“事件處理程式”的使用也是如此,因為它在使用者單擊前端上的按鈕時提供某種形式的通知。因此,我們可以認為 CallBack 函式在執行常規程式設計任務(例如執行各種操作和從網路下載一些資料)時發揮著至關重要的作用。至於在 JAVA 程式語言中使用 CallBack 屬性,可以透過使用介面來實現。讓我們回顧一下在 JAVA 程式設計中執行 CallBack 函式的常用方法。

回撥功能的實現方法?

  • 我們首先定義我們需要在回撥後呼叫的介面方法。

  • 要實現介面回撥方法,請定義一個類。

  • 要註冊回撥介面,請設定對任意類的引用。

  • 使用已定義的引用來呼叫 CallBack 方法。

Java 中的回撥型別?

  • 同步呼叫

  • 非同步呼叫

同步呼叫語法

  • 步驟 1 - 建立一個介面。

  • 步驟 2 - 建立 void 方法。

  • 步驟 3 - 建立類。

  • 步驟 4 - 建立 public void 方法。

示例

// This is a program in Java for understanding
// the implementation of "Asynchronous call".

interface ExampleEventListener {
   // Any type of method
   // can be defined below
   void exampleEvent();
}

// Define a random class
// in which the main section is also
// defined
public class Example2 {
   private ExampleEventListener mListener; // This is the listener field

   // Set up the Listener below using the public
   // access modifier and the void return type
   public void registerExampleEventListener(ExampleEventListener mListener) {
      this.mListener = mListener;
   }

   // This is my synchronous task
   // which needs to be performed.
   public void myStuff() {

      // An async task always executes within a new thread
      // so a new thread must be created in order to execute.

      System.out.print("The task being performed ");
      System.out.println("here is a synchronous task");

      // Check whether the listener is registered or not.
      if (this.mListener != null) {
         // Invoke the callback method of the ExampleEventListener class
         mListener.exampleEvent();
      }
   }

   public static void main(String[] args) {
      Example2 obj = new Example2();
      ExampleEventListener mListener = new Myclass();
      obj.registerExampleEventListener(mListener);
      obj.myStuff();
   }
}

class Myclass implements ExampleEventListener {
   @Override
   public void exampleEvent() {
      System.out.print("Performing callback after ");
      System.out.print("the completion of the synchronous task");
      // Perform any random routine operation
   }
   // Some Myclass methods
}

輸出

The task being performed here is a synchronous task
Performing callback after the completion of the synchronous task

從重要的輸出中,我們可以理解在 JAVA 程式語言中使用 CallBack 方法的同步呼叫的作用。我們可以觀察到上面討論的每個步驟都與上面的程式相關聯。在上文中,我們在程式中使用了事件偵聽器介面。

何時使用同步呼叫?

當程式中的多個任務需要按特定順序依次執行時,我們使用同步呼叫。耗時較短的任務也被認為是同步呼叫的。

非同步呼叫語法

  • 步驟 1 - 建立一個介面。

  • 步驟 2 - 建立一個 void 類。

  • 步驟 3 - 建立一個 public void 類。

  • 步驟 4 - 建立 public void 方法。

示例

interface ExampleCallEventListener {
   void exampleCallEvent();
}

public class Example1 {

   private ExampleCallEventListener mListener;

   public void registerExampleCallEventListener(ExampleCallEventListener mListener) {
      this.mListener = mListener;
   }

   public void myStuff() {
      new Thread(new Runnable() {
         public void run() {
            System.out.println("This is an example and tutorial here");

            if (mListener != null) {
               mListener.exampleCallEvent();
            }
         }
      }).start();
   }

   public static void main(String[] args) {
      Example1 obj = new Example1();
      ExampleCallEventListener mListener = new MyClass();
      obj.registerExampleCallEventListener(mListener);
      obj.myStuff();
   }
}

class MyClass implements ExampleCallEventListener {
   
   @Override
   public void exampleCallEvent() {
      System.out.println("Performing callback after the completion of an Async Task");
   }
}

輸出

This is an example and tutorial here
Performing callback after the completion of an Async Task

從上面提到的程式中,我們可以瞭解到在 JAVA 程式語言中使用 CallBack 方法的非同步呼叫的作用。我們可以觀察到建立了一個新執行緒,並且回撥是在該執行緒內呼叫的。我們在程式中使用了事件偵聽器介面。

何時使用非同步呼叫?

當程式任務彼此不依賴時,我們使用非同步呼叫,因此一個任務的中斷不會影響其他任務。耗時較長的任務也被認為是非同步呼叫的。

Java 中同步呼叫和非同步呼叫的區別

定義

同步呼叫是一種回撥,其中執行程式碼在繼續之前等待事件。另一方面,非同步呼叫是指不阻塞事務的回撥。這是 Java 中同步呼叫和非同步呼叫的主要區別。

功能

Java 既有同步呼叫也有非同步呼叫,它們在等待事件時處理程式碼執行的方式不同。與同步呼叫或回撥不同,非同步呼叫允許程式在此等待期間繼續執行程式碼。

應用

程式設計師可以根據他們對任務完成的需求訪問兩種不同的回撥方法——同步或非同步,這取決於哪種方法最適合他們。同步回撥在執行需要最少處理時間的例行任務時非常方便,而非同步回撥則更適合執行時間較長的更復雜和不相關的程序。類似地,Java 基於編碼實踐中的同步與非同步呼叫也存在差異,沒有例外。

結論

實現 Java 程式設計允許使用同步和非同步呼叫。與非同步呼叫執行不同,非同步呼叫執行允許程式無中斷地執行其他程式碼段,同步呼叫需要事件發生後系統或程式才能採取進一步的操作。

更新於:2023年8月1日

3K+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告