Skip to content

Latest commit

 

History

History
96 lines (85 loc) · 5.09 KB

README.md

File metadata and controls

96 lines (85 loc) · 5.09 KB

Maven Central Gitter Coverage Status Build Status License: LGPL v3

Maven Central Dependency

To import this library into Maven, simply insert the following dependency in your pom.xml:

  <dependencies>
    <dependency>
      <groupId>com.nealk.concurrent</groupId>
      <artifactId>concurrent-tasks</artifactId>
      <version>1.10</version>
    </dependency>
  </dependencies>

Concurrent Tasks Java Library

The Concurrent Tasks Library is an easy-to-consume Java concurrency library allowing for "Tasks" to execute business logic in a thread safe manner. This library helps users achieve multi-threading in their applications without worrying about synchronization and blocking for race conditions. Presently, there are 2 types of Tasks: Retrievable and Non-Retrievable.

Retrievable Tasks

Once RetrievableTask is extended, this allows for @ThreadSafe concurrent execution where business logic executed does need to return back an object. As a result, calling the getVal() method for a Retrievable task returns the object of type T (via use of Java generics) - which is blocked until all logic in the execute() method has terminated.

Example usages: Api calls, I/O, or any other situation warranting concurrent parallel processing.

Note that classes extending RetrievableTask.java should explicity specify object type in the class declaration to enable compile-type type checking. See example:

  public class ExampleTask extends RetrievableTask<String>{}

Finally to return the value, one simple has to set the value for obj or this.obj, as such:

  public class ExampleTask extends RetrievableTask<String>{
      @Overide
      protected void execute(){
        //do work, execute business logic
        
        //then set the return value for ExampleTasks's object
        this.obj = "Example task successfully completed";
      }
  }

Non-Retrievable Tasks

Once NonRetrievableTask has been extended, this allows for simple concurrent execution where the business logic executed does not need to return back an object. As a result, calling the getVal() method for a NonRetrievableTask throws an java.lang.UnsupportedOperationException.

Example usages: initializers, message dispatchers, or any standalone time-consuming task which you would like to execute concurrently.

Client Usage Examples

Regardless on the type of Task needed, each respective Task should be wrapped within with a Thread and will begin execution upon calling of the start() or run() methods. The start() method executes the Task in a new thread, while the run() method executes the Task in the same thread.

Below is an example of a RetrievableTask and NonRetrievable executing business logic in new threads, and then printing out status messages - based on the designated Task type.

  public class Main{
  
    private Task retrievable, nonRetrievable;
    
    public static void main(String[] args){
      retrieveable = new RTask();
      nonRetrievable = new NRTask();
      
      //start the Retrievable Task
      Thread t = new Thread(retrievable);
      t.start();
      //start the Non-Retrievable Task using an anonymous Thread                   
      new Thread(nonRetrievable).start();
      
      //Print the results of each respective Task
      System.out.println(retrievable.getVal()); // Since this is a RetrievableTask, retrievable.getVal()
                                                // is blocked until its execute() method has completed.
    }
    
    private static class RTask extends RetrievableTask<String>{
      @Override
      protected void execute(){
        //do work, execute business logic
        //...    
        this.obj = "Finished with Retrievable task!";
      }
    }
    
    private static class NRTask extends NonRetrievableTask{
      @Override
      protected void execute(){
        //do work, execute business logic
        //...
        System.out.println("Finished with Non-Retrievable task!");
      }
    } 
    
  }

Console:

  Thread #2 started...
  Thread #1 started...
  Finished with Retrievable task!
  Finished with Non-Retrievable task!