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.
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
);
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);
}
});
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]
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" ]