0% found this document useful (0 votes)
137 views11 pages

Java Multithreading and Concurrency Training

This document provides an overview of multithreading and concurrency in Java. It discusses key concepts like single threads versus multithreading, thread life cycles, the difference between Runnable and Thread, and how to implement thread pools, callables, and futures. Specific topics covered include thread safety, deadlocks, thread pools using Executors, callables that can return values, and using futures to retrieve results from callable tasks. Examples are provided throughout to illustrate various multithreading techniques in Java.

Uploaded by

Ghanshyam Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views11 pages

Java Multithreading and Concurrency Training

This document provides an overview of multithreading and concurrency in Java. It discusses key concepts like single threads versus multithreading, thread life cycles, the difference between Runnable and Thread, and how to implement thread pools, callables, and futures. Specific topics covered include thread safety, deadlocks, thread pools using Executors, callables that can return values, and using futures to retrieve results from callable tasks. Examples are provided throughout to illustrate various multithreading techniques in Java.

Uploaded by

Ghanshyam Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Java Multithreading and

Concurrency Training
Multithreading and Concurrency
› What is Single Thread
› What is Multithreading in Java?
› Multithreading in Java
› Threads in Java
› Java Thread Example
› Java Thread Sleep
› Java Thread Join
› Java Thread States
› Java Thread wait, notify and notifyAll
› Java Thread Safety and Java Synchronization

Ericsson | Commercial in Confidence | 2020-09-09 | Page 2


Ericsson | Commercial in Confidence | 2020-09-09 | Page 3
Ericsson | Commercial in Confidence | 2020-09-09 | Page 4
Thread Life Cycle

Ericsson | Commercial in Confidence | 2020-09-09 | Page 5


Runnable vs Thread


If your class provides more functionality rather than just running as Thread, you
should implement Runnable interface to provide a way to run it as Thread. If
your class only goal is to run as Thread, you can extend Thread class.

Implementing Runnable is preferred because java supports implementing


multiple interfaces. If you extend Thread class, you can’t extend any other
classes.

Ericsson | Commercial in Confidence | 2020-09-09 | Page 6


Multithreading and Concurrency

› Java Exception in thread main


› Thread Safety in Singleton Class
› Daemon Thread in Java
› Java Thread Local
› Java Thread Dump
› How to Analyze Deadlock and avoid it in Java
› Java Timer Thread
› Java Producer Consumer Problem
› Java Thread Pool
› Java Callable Future
› Java FutureTask Example
Ericsson | Commercial in Confidence | 2020-09-09 | Page 7
Java thread pool
› Java thread pool manages the pool of worker threads. It contains a queue that
keeps tasks waiting to get executed. We can use ThreadPoolExecutor to create
thread pool in Java.

› Java thread pool manages the collection of Runnable threads. The worker
threads execute Runnable threads from the queue.
java.util.concurrent.Executors provide factory and support methods for
java.util.concurrent.Executor interface to create the thread pool in java.

› Executors is a utility class that also provides useful methods to work with
ExecutorService, ScheduledExecutorService, ThreadFactory, and Callable
classes through various factory methods.

Ericsson | Commercial in Confidence | 2020-09-09 | Page 8


Java Callable and Future
› Java Callable and Future are used a lot in multithreaded programming. In last
few posts, we learned a lot about java threads but sometimes we wish that a
thread could return some value that we can use. Java 5
introduced java.util.concurrent.Callable interface in concurrency package that
is similar to Runnable interface but it can return any Object and able to throw
Exception.
Java 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.

Ericsson | Commercial in Confidence | 2020-09-09 | Page 9


Java Future
Java Future
› Java Callable tasks return java.util.concurrent.Future object. 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.

› Java Future provides cancel() method to cancel the associated Callable task. There is an overloaded
version of get() method where we can specify the time to wait for the result, it’s useful to avoid current
thread getting blocked for longer time. There are isDone() and isCancelled() methods to find out the current
status of associated Callable task.

› Here is a simple example of Java Callable task that returns the name of thread executing the task after one
second. We are using Executor framework to execute 100 tasks in parallel and use Java Future to get the
result of the submitted tasks.

Ericsson | Commercial in Confidence | 2020-09-09 | Page 10


Ericsson | Commercial in Confidence | 2020-09-09 | Page 11

You might also like