Git Product home page Git Product logo

task4java's Introduction

task4java

task4java is a framework to compose and execute code asynchronously. Its features are:

  • lightweight
  • high performance
  • based on standard Java libraries (Java V1.6 and above)
  • Android compatible
  • Lambda compatible (Java V1.8)

The key concept is based on JS promises (e.g. when.js) or Microsoft .NET Task Parallel Library (TPL).

The major class of this framework is the Task class which is based on the Java FutureTask class.

Usage

The following code will show the execution of three code blocks in series.

Task<String> taskResult = TaskFactory.startNew(new Callable<String>() {

	@Override
	public String call() throws Exception {

		// Schedule a new task on the default thread pool
		Logger.instance.d(TAG, "Thread for task1: " + Thread.currentThread().getId());

		Thread.sleep(1000);

		return "Ready";
	}
}).continueWith(new CallableTask<String, String>() {

	@Override
	public String call(Task<String> task) throws Exception {

		// Execute this code after the previous task has finished
		Logger.instance.d(TAG, "Thread for step1: " + Thread.currentThread().getId());

		return task.get() + " step1";
	}
}).continueWith(new CallableTask<String, String>() {

	@Override
	public String call(Task<String> task) throws Exception {

		// Execute this code after the previous task has finished
		Logger.instance.d(TAG, "Thread for step2: " + Thread.currentThread().getId());

		return task.get() + " step2";
	}
});

// blocks until the result is available
System.out.println(taskResult.get());

To change the executor of the tasks the continueWith method is overloaded with the following signatures:

public <VNew> Task<VNew> continueWith(CallableTask<VNew, V> callableTask);
public <VNew> Task<VNew> continueWith(CallableTask<VNew, V> callableTask, Executor executor)

Hence each Executor can be used to start the tasks. The TaskFactory class contains some executor services but you can create your own executor if necessary.

License

Licensed under Apache license 2.0. Full license here »

task4java's People

Contributors

ahagelstein avatar

Watchers

James Cloos avatar Juan Manuel Romero Martin avatar

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.