Jetlang是Java高并发Actor模型开源库包

16-02-24 banq
                   

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>

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>

项目地址:

GitHub - jetlang/core

                   

1