Java supports low-level of concurrency and some of rich api’s which is just a wrapper of low-level constructs like wait, notify, synchronize etc, But with Java concurrent packages Scala have high level concurrency frameworks for “which goal to achieve, rather than how to achieve“. These types of programming paradigms are “Asynchronous programming using Futures“, “Reactive programming using event streams“, “Actor based programming” and more.
Note: Thread creation is much more expensive then allocating a single object, acquiring a monitor lock or updating an entry in a collection.
For high-performance multi-threading application we should use single thread for handling many requests and a set of such reusable threads usually called thread pool.
In java, Executor is a interface for encapsulate the decision of how to run concurrently executable work tasks, with an abstraction. In other words, this interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc
Java Executor :
Note: java.util.concurrent.Executors is a utility class, which is used to create create thread pool according to requirements.
References:
Note: Thread creation is much more expensive then allocating a single object, acquiring a monitor lock or updating an entry in a collection.
For high-performance multi-threading application we should use single thread for handling many requests and a set of such reusable threads usually called thread pool.
In java, Executor is a interface for encapsulate the decision of how to run concurrently executable work tasks, with an abstraction. In other words, this interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc
interface Executor{ public void execute(Runnable command) }
Java Executor :
- Executor decide on which thread and when to call run method of Runnable object.
- Executor object can start a new thread specifically for this invocation of execute or even the execute the Runnable object directly on the caller thread.
- Tasks scheduling is depends on implementation of Executor.
- ExecutorService is a sub interface of Executor for manage termination and methods that can produce a future for tracking progress of one or more asynchronous tasks.
- In Java some basic Executor implementations are ThreadPoolExecutor (JDK 5), ForkJoinPoll (JDK 7) etc or developers should are provide custom implementation of Executor.
trait ExecutionContext { def execute(runnable: Runnable): Unit def reportFailure(cause: Throwable): Unit }Scala ExecutionContext:
- ExecutionContext also have an companion object which have some methods for creating ExecutionContext object from Java Executor or ExecutorService (act as a bridge between Java and Scala)
- ExecutionContext companion object contains the default execution context called global which internally uses a ForkJoinPool instance.
Note: java.util.concurrent.Executors is a utility class, which is used to create create thread pool according to requirements.
References:
- Learning Concurrent Programming in Scala by Aleksandar Prokopec.
- Java api’s docs.
This comment has been removed by a blog administrator.
ReplyDelete