Git Product home page Git Product logo

futurity's Introduction

futurity Build Status Maven Central

A simple tool to convert plain old Java Future to CompletableFuture

How to use?

<dependency>
    <groupId>com.spikhalskiy.futurity</groupId>
    <artifactId>futurity-core</artifactId>
    <version>0.3-RC3</version>
</dependency>
Future oldFuture = ...;
CompletableFuture profit = Futurity.shift(oldFuture);

Motivation

You have an old code, which is asynchronous, but uses pre Java 8 API and you want to convert Future to CompletableFuture to get a full power of the new api.

It could require an update of 3rd party dependencies. And even in this case it could be a bit tricky if a library that performs IO doesn't support nor CompletableFuture neither callback API. If you have large amount of own code under Future - it could require big refactoring to introduce callbacks or a CompletableFuture model.

The best that you can do without large immediate reworking from related StackOverflow discussion is:

public static <T> CompletableFuture<T> makeCompletableFuture(Future<T> future) {
    return CompletableFuture.supplyAsync(() -> {
        try {
            return future.get();
        } catch (ExecutionException e) {
            throw new CompletionException(e.getCause());
        } catch (InterruptedException e) {
            throw new CompletionException("Interrupted");     
        }
    });
}

Which is very bad solution, because a thread will be blocked and wait for the Future result. What if it's a thread from common pool? Mostly possible you didn't pass special executor. What if you have a lot of such futures? Thread pool could be quickly exhausted and you steal common resources from useful work for just active future checking.

This library provides a better way - it maintains collections of passed futures inside and returns CompletableFuture outside that gets value when Future would be done. For checking all passed futures futurity uses only one thread.

Futurity could be a good choice for a migration and to be a mediator between new code that wants to use all features provided by CompletableFuture (CompletionStage) and old code, which supports plain Future only. Futurity is either good for tests code where you don't want to spend much time for supporting CompletableFuture properly and want to leave code with Futures and use it outside in CompletableFuture way.

Caution

You shouldn't consider this library to create new good things and new APIs. Inside this library you still have active waiting on futures and waste resources on it. Futurity just makes it much cheaper than straightforward active waiting on each future in CompletableFuture.supplyAsync. Plan your new code right and use appropriate underlying libraries that doesn't require active checking of futures by supporting callbacks of CompletableFuture APIs.

futurity's People

Contributors

spikhalskiy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

futurity's Issues

Add ability to create Futurity instance with specified parameters

In addition to main static instance we should have an ability to create task-specific Futurity. This would make it possible to have multiple Futurity instances set up for different use cases.

For example, it's fine to check Future for downloading file from external portal in background each 100 ms, while internal tasks should be checked each 1 ms.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.