Skip to content

mbouchenoire/jsync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Drawing Jsync

Build Status  Coverage Status

Jsync is a lightweight Java library focused on providing simple methods to deal with concurrency before the Java 8 Stream API. This library is mostly influenced by the .NET Parallel Class and async.js.

Methods

parallel(Runnable[] runnables)

Call each given Runnable asynchronously while beeing synchronous itself. You can pass as many runnables as you want using varargs, or even provide them within an Array / Collection.

Jsync.parallel(
    new Runnable() {
        public void run() {
            // Do some long task here...
        }
    },
    new Runnable() {
        public void run() {
            // Do some other long task there...
        }
    },
    new CustomRunnable(args) // or implement your own runnables
);

forEach(T[] items, Consumer<T> consumer)

Apply each value in items to the Consumer. Each execution of Consumer#accept() is called asynchronously while the forEach() method itself is synchronous.

String[] strings = new String[] { "hi", "jsync" };

Jsync.forEach(strings, new Consumer<String>() {
    public void accept(String arg) {
        // each execution of this function is asynchronous
        System.out.println(arg);
    }
});

T[] map(T[] items, Function<T, R> function)

Produces a new Array / Collection of values by mapping each value in items through the Function. Each execution of Function#apply() is called asynchronously while the map() function itself is synchronous.

String[] strings = new String[] { "hi" , "Jsync" };

Integer[] lengths = Jsync.map(strings, new Function<String, Integer>() {
    public Integer apply(String arg) {
        // each execution of this function is asynchronous
        return arg.length();
    }
});

// lengths : [2, 5]

T[] filter(T[] items, Predicate<T> predicate)

Produces a new Array / Collection of values which pass the Predicate test. Each execution of Predicate#test() is called asynchronously while the filter() function itself is synchronous.

String[] strings = new String[] { "hi", "jsync", "this is too long" };

String[] filteredStrings = Jsync.filter(strings, new Predicate<String>() {
    public Boolean test(String arg) {
        // each execution of this function is asynchronous
        return (arg.length <= 10);
    }
});

// filteredStrings : [ "hi", "jsync" ]

Releases

No releases published

Packages

No packages published

Languages