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.
2. Agenda
● What is concurrency?
● About Concurrent Package
● Thread Pool
● Executor
● Future
● Callable
● Demo
3. 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.
4. 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
5. 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.
7. The ExecutorService has the following
implementation in concurrent package:
● ThreadPoolExecutor
● ScheduledThreadPoolExecutor
8. 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.
9. 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.