java concurrent
TRANSCRIPT
Java Concurrent
Anubhav Goyal
Agenda
What is concurrency?
About Concurrent Package
Thread Pool
Executor
Future
Callable
Demo
Concurrency
Concurrency is the ability to run several programs or several parts of a program in parallel. If a time consuming task can be performed asynchronously or in parallel, this improve the throughput and the interactivity of the program.
Councurrent Package
It released with jdk 1.5. It contains many classes and interfaces for threading. There try to overcome from the limits of traditional Threading. There are more pacakges inside java.util.concurrent package, these are:java.util.concurrent.atomic
java.util.concurrent.locks
Thread Pool
Thread Pools are useful when you need to limit the number of threads running in your application at the same time. There is a performance overhead associated with starting a new thread, and each thread is also allocated some memory for its stack etc.
Executor
Executors framework (java.util.concurrent.Executor), released with the JDK 5 in package java.util.concurrent is used to run the Runnable objects without creating new threads every time and mostly re-using the already created threads.
The ExecutorService has the following implementation in concurrent package:ThreadPoolExecutor
ScheduledThreadPoolExecutor
Future
When we submit a long running task to ExecutorService, it returns a Future object immediately. This Future object can be used to query task completion and getting result of computation.Using Java Future object, we can find out the status of the Callable task and get the returned Object. It provides get() method that can wait for the Callable to finish and then return the result.
Callable
Java Callable interface use Generic to define the return type of Object. Executors class provide useful methods to execute Java Callable in a thread pool. Since callable tasks run in parallel, we have to wait for the returned Object.
Link for demo
https://github.com/NexThoughts/Java-Concurrent