Jetlang是Java高併發Actor模型開源庫包

banq發表於2016-02-24
Jetlang基於Retlang提供高效能的Java執行緒庫包,可作為JDK的java.util.concurrent庫包補充實現,它類似Scala中Actor一樣,是基於訊息的高併發庫包。

這個庫包不提供遠端訊息能力,設計用於基於單臺機器記憶體in-memory的高效能併發:

1.所有訊息將順序遞交到一個特殊的纖程Fiber(類似ErLang和GO的輕量執行緒),庫包元件將無需任何同步鎖訪問和不會產生任何資源競爭情況下很方便地改變和持有狀態。

2.單個Fiber介面可透過定製的一個執行緒或執行緒池來支援。

3.支援訊息的單個或多個訂閱者。

4.支援單個事件或批次事件的訂閱

5.單個或迴圈事件的排程

6.對低延遲和高擴充套件性進行最佳化的高效能設計

7.對外發布使用是執行緒安全的,易於和其他執行緒模型整合。

8.低鎖化競爭,將鎖競爭降低到最小化是效能關鍵,一般的執行緒池或訊息佇列的解決方案總是最後在效能上受限於一個單個鎖競爭,而Jetlang沒有這樣的中央瓶頸,效能容易擴充套件。

9.強大的非同步請求和回覆支援

使用方式:

Fiber fiber = new ThreadFiber();
fiber.start();
final CountDownLatch latch = new CountDownLatch(2);
Runnable toRun = new Runnable(){
    public void run(){
        latch.countDown();
    }
};
//enqueue runnable for execution
fiber.execute(toRun);
//repeat to trigger latch a 2nd time
fiber.execute(toRun);
latch.await(10, TimeUnit.SECONDS);
//shutdown thread
fiber.dispose();
<p class="indent">


Channel方式使用:

// start thread backed receiver. 
    // Lighweight fibers can also be created using a thread pool
    Fiber receiver = new ThreadFiber();
    receiver.start();

    // create java.util.concurrent.CountDownLatch to notify when message arrives
    final CountDownLatch latch = new CountDownLatch(1);

    // create channel to message between threads
    Channel<String> channel = new MemoryChannel<String>();

    Callback<String> onMsg = new Callback<String>() {
        public void onMessage(String message) {
            //open latch
            latch.countDown();
        }
    };
    //add subscription for message on receiver thread
    channel.subscribe(receiver, onMsg);

    //publish message to receive thread. the publish method is thread safe.
    channel.publish("Hello");

    //wait for receiving thread to receive message
    latch.await(10, TimeUnit.SECONDS);

    //shutdown thread
    receiver.dispose();
<p class="indent">


專案地址:

GitHub - jetlang/core

相關文章