concurrent


Source link: https://github.com/onehilltech/concurrent

concurrent

Utility library of concurrent execution strategies for JVM and Android

  • Inspired by async for JavaScript and Node.js.
  • Execute collection iterators and control flow strategies concurrently in the background.
  • Callbacks are notified when strategies are complete, cancelled, or error out.
  • Android extensions execute final callbacks on the UI thread.

Installation

Gradle

buildscript {

repositories {

  maven {
 url "https://jitpack.io" 
}

}
 
}
  dependencies {

# Select the dependency based on your project. You must not specify both modules
# in your project.

# for JVM projects
compile 'com.github.onehilltech.concurrent:concurrent-core:x.y.z'

# for Android projects
compile 'com.github.onehilltech.concurrent:concurrent-android:x.y.z' 
}
 

where x.y.z is the latest version:

Quick Start

Each strategy is implemented atop an Executor object. There are two ways to execute a concurrent strategy. The first method is to use the default executor:

Concurrent.getDefault ().series (
new Task ("task-1") {

  @Override
  public void run (Object unused, CompletionCallback callback) {

 callback.done ("1");

  
}

}
,
new Task ("task-2") {

  @Override
  public void run (Object unused, CompletionCallback callback) {

 callback.done ("2");

  
}

  
}
).execute (new CompletionCallack <Map <String, Object>> () {

  @Override
  public void onComplete (Map <String, Object> result) {

 // result.get ("task-1") equals "1"

 // result.get ("task-2") equals "2"
  
}

@Override
  public void onFail (Throwable e)
  {

 // one of the tasks failed
  
}

@Override
  public void onCancel ()
  {

 // the series of tasks where cancelled
  
}

  
}
);

The second method is to create the concurrent strategy on an existing Executor object:

Series series = new Series (executor,
new Task ("task-1") {

  @Override
  public void run (Object unused, CompletionCallback callback) {

 callback.done ("1");

  
}

}
,
new Task ("task-2") {

  @Override
  public void run (Object unused, CompletionCallback callback) {

 callback.done ("2");

  
}

  
}
).execute (new CompletionCallack <Map <String, Object>> () {

  @Override
  public void onComplete (Map <String, Object> result) {

 // result.get ("task-1") equals "1"

 // result.get ("task-2") equals "2"
  
}

@Override
  public void onFail (Throwable e)
  {

 // one of the tasks failed
  
}

@Override
  public void onCancel ()
  {

 // the series of tasks where cancelled
  
}

  
}
);

Chaining Concurrent Strategies

Concurrent strategies can be chained together. This is because the CompletionCallback parameter in the Task.run method can be passed as the CompletionCallback parameter to the Task.execute method.

Here is an example of downloading a result, and then iterating over the values in the result:

Concurrent.getDefault ().waterfall (
new Task ("download-users") {

  @Override
  public void run (Object lastResult, CompletionCallback callback) {

 List <User> users;

  // GET /users

  callback.done (users);

  
}

}
,
new Task ("save-users") {

  @Override
  public void run (Object lastResult, CompletionCallback callback) {

 List <User> users = (List <User>)lastResult;

  Concurrent.getDefault ().forEach (

new Task <User> () {

  @Override

  public void run (User user, CompletionCallack callback) {

 // save the user

 callback.done ();

  
}

}
).execute (users, callback);

  
}

  
}
).execute (new CompletionCallack <Map <String, Object>> () {

  @Override
  public void onComplete (Object lastResult) {

  // the tasks are complete
  
}

@Override
  public void onFail (Throwable e)
  {

 // one of the tasks failed
  
}

@Override
  public void onCancel ()
  {

 // the series of tasks where cancelled
  
}

  
}
);

Android Support

Use AndroidCompletionCallback with the initial execute method to ensure the completion methods run on the main UI thread.

Next Steps

For more details on the strategies supports in Concurrent, please refer to the documentation of async. Each strategy in Concurrent mirrors the behavior of its corresponding strategy in async.

Happy Coding!

Resources

This is a fork of the java-mail api and javax.activation, which is made "Android ready".

This is library to easily use connection between android wear and handheld device.

A simple android library that lets user select a directory.

Android library to backport Material design and allow changing colors at run-time.

Small library that provides... bouncing dots. This feature is used in number of messaging apps (such as Hangouts or Messenger), and lately in Android TV (for example when connecting to Wifi).

A small library including an example app which uses the "floating label" pattern to show form validation.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes