java event-loop

a pure lightweight event-loop.

License

License

GroupId

GroupId

com.zmannotes
ArtifactId

ArtifactId

event-loop
Last Version

Last Version

1.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

java event-loop
a pure lightweight event-loop.
Project URL

Project URL

https://github.com/zman2013/event-loop
Source Code Management

Source Code Management

https://github.com/zman2013/event-loop

Download event-loop

How to add to project

<!-- https://jarcasting.com/artifacts/com.zmannotes/event-loop/ -->
<dependency>
    <groupId>com.zmannotes</groupId>
    <artifactId>event-loop</artifactId>
    <version>1.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.zmannotes/event-loop/
implementation 'com.zmannotes:event-loop:1.0.2'
// https://jarcasting.com/artifacts/com.zmannotes/event-loop/
implementation ("com.zmannotes:event-loop:1.0.2")
'com.zmannotes:event-loop:jar:1.0.2'
<dependency org="com.zmannotes" name="event-loop" rev="1.0.2">
  <artifact name="event-loop" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.zmannotes', module='event-loop', version='1.0.2')
)
libraryDependencies += "com.zmannotes" % "event-loop" % "1.0.2"
[com.zmannotes/event-loop "1.0.2"]

Dependencies

test (3)

Group / Artifact Type Version
ch.qos.logback : logback-classic jar 1.2.3
junit : junit jar 4.12
org.mockito : mockito-core jar 3.1.0

Project Modules

There are no modules declared in this project.

Travis Build Coverage Status

event-loop

a pure lightweight event-loop based on a single thread

Dependency

<dependency>
    <groupId>com.zmannotes</groupId>
    <artifactId>event-loop</artifactId>
    <version>1.0.2</version>
</dependency>

Interface

/**
 * eventloop一旦创建就会开始运行loop,无需start,直接可以submit任务
 */
public interface EventLoop {

    /**
     * 提交任务
     * @param taskType 事件类型,用于区分不同类型的任务,用于做任务调度
     * @param task     任务
     * @param <T>      返回值的泛型
     * @return  future
     */
    <T> Future<T> submit(String taskType, Callable<T> task);

    /**
     * 提交任务
     * @param taskType  事件类型,用于区分不同类型的任务,用于做任务调度
     * @param task      任务
     * @param timeout   时间
     * @param timeUnit  单位
     * @param <T>       返回值的泛型
     * @return  future
     */
    <T> Future<T> submit(String taskType, Callable<T> task, long timeout, TimeUnit timeUnit);

    /**
     * 停止event loop
     * @return future
     */
    Future<?> shutdown();
}

Example

EventLoop eventLoop = new DefaultEventLoop("event-loop");
Future<Long> future = eventLoop.submit(TaskType.COMPUTE.name(), ()-> 1+1L);
Assert.assertEquals(2L, future.get().longValue());

Future<Long> future2 = eventLoop.submit(TaskType.COMPUTE.name(), ()-> 1+1L, 10, TimeUnit.MILLISECONDS);
Assert.assertEquals(2L, future2.get(100, TimeUnit.MILLISECONDS).longValue());

AtomicInteger c = new AtomicInteger(0);
eventLoop.submit(()->c.incrementAndGet());
eventLoop.submit(()->c.incrementAndGet(),1,TimeUnit.NANOSECONDS);

Versions

Version
1.0.2
1.0.1
0.0.2
0.0.1