fiber4j

Co-operative multitasking utility

License

License

GroupId

GroupId

com.github.akurilov
ArtifactId

ArtifactId

fiber4j
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

fiber4j
Co-operative multitasking utility
Project URL

Project URL

https://github.com/akurilov/fiber4j
Source Code Management

Source Code Management

https://github.com/akurilov/fiber4j.git

Download fiber4j

How to add to project

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

Dependencies

compile (1)

Group / Artifact Type Version
com.github.akurilov : java-commons jar 2.2.0

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

Introduction

The library supporting the cooperative multitasking. Introduces the fibers which are useful to execute a lot of periodic/long tasks using a fixed count of the threads.

In the context of the library, the fiber is a stoppable task which executes for some very short time avoiding any blocks multiple times (in the reentrant way). A basic fiber instance may be executing by several different threads at any moment of time so it should have thread-safe user code. To implement the protected, thread-safe fiber the special ExclusiveFiberBase class is provided. An exclusive fiber is being executed by only one thread at any moment of time.

The fibers are being executed by the fibers executor. Any fiber executor has the thread-safe registry. The fibers executor threads iterate the fibers registry and invoke the fibers sequentially. As far as fibers executor is multithreaded the fibers are being executed concurrently also.

Usage

Gradle

compile group: 'com.github.akurilov', name: 'fiber4j', version: '1.1.0'

Implementing Basic Fiber

To implement the simplest fiber one should extend the FiberBase class:

package com.github.akurilov.fiber4j.example;

import com.github.akurilov.fiber4j.FiberBase;
import com.github.akurilov.fiber4j.FibersExecutor;

public class HelloWorldFiber
extends FiberBase {

    public HelloWorldFiber(final FibersExecutor fibersExecutor) {
        super(fibersExecutor);
    }

    @Override
    protected void invokeTimed(final long startTimeNanos) {
        System.out.println("Hello world");
    }

    @Override
    protected void doClose()
    throws IOException {

    }
}

The method invokeTimed does the useful work. The example code below utilizes that fiber:

package com.github.akurilov.fiber4j.example;

import com.github.akurilov.fiber4j.Fiber;
import com.github.akurilov.fiber4j.FibersProcessor;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class Main {

    public static void main(final String... args)
    throws InterruptedException, IOException {

        final FibersExecutor fibersExecutor = new FibersExecutor();
        final Fiber helloFiber = new HelloWorldFiber(fibersExecutor);
        helloFiber.start();
        TimeUnit.SECONDS.sleep(10);
        helloFiber.close();
    }
}

invokeTimed notes

The code executed in the invokeTimed method should follow the rules:

  • Do not block if possible
  • Take care of own thread safety
  • Do not exceed the timeout (Fiber.TIMEOUT_NANOS)

The invoked code should take the responsibility on the time of its execution. Example:

    @Override
    protected void invokeTimed(long startTimeNanos) {
        for(int i = workBegin; i < workEnd; i ++) {
            doSomeUsefulWork(i);
            // yes, I know that the statement below may invoke Satan
            // but for simplicity it doesn't expect the negative result
            if(System.nanoTime() - startTimeNanos > TIMEOUT_NANOS) {
                break;
            }
        }
    }

Implementing Exclusive Fiber

An exclusive fiber is restricted by a single thread. It allows:

  • Consume less CPU resources (useful for "background" tasks)
  • Don't care of thread safety
package com.github.akurilov.fiber4j.example;

...
import com.github.akurilov.fiber4j.ExclusiveFiberBase;

public class HelloWorldExclusiveFiber
extends ExclusiveFiberBase {

    ...

    @Override
    protected void invokeTimedExclusively(long startTimeNanos) {
        System.out.println("Hello world!");
    }
    ...

Other Fiber Implementations

There are some other fiber implementations included into the library for the user reference. These fibers are used in the Mongoose project widely and proved the fibers approach efficiency.

Versions

Version
1.1.0
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0