Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Multithreading in Java (Part3)

This document discusses multithreading in Java. It covers deadlocks, how to avoid them, and the ExecutorService interface. Deadlocks occur when threads wait for each other to release locks. To avoid deadlocks, avoid nested and unnecessary locks, and use thread joining with timeouts. The ExecutorService interface is similar to a thread pool and has implementations like ThreadPoolExecutor and ScheduledThreadPoolExecutor. ExecutorServices can be created using the Executors factory class.

Uploaded by

Mubariz Mirzayev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Multithreading in Java (Part3)

This document discusses multithreading in Java. It covers deadlocks, how to avoid them, and the ExecutorService interface. Deadlocks occur when threads wait for each other to release locks. To avoid deadlocks, avoid nested and unnecessary locks, and use thread joining with timeouts. The ExecutorService interface is similar to a thread pool and has implementations like ThreadPoolExecutor and ScheduledThreadPoolExecutor. ExecutorServices can be created using the Executors factory class.

Uploaded by

Mubariz Mirzayev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Multithreading in Java

(Part3)
Sarkhan Naghiyev 07/18/2023
Resources
https://www.geeksforgeeks.org/deadlock-in-java-multithreading/?ref=lbp

https://www.geeksforgeeks.org/lock-framework-vs-thread-synchronization-in-java/?ref=lbp

https://www.geeksforgeeks.org/difference-between-lock-and-monitor-in-java-concurrency/?ref=lbp

https://jenkov.com/tutorials/java-util-concurrent/executorservice.html

https://www.javatpoint.com/interrupting-a-thread

https://www.javatpoint.com/java-thread-pool

https://www.javatpoint.com/java-executorservice

https://dzone.com/articles/java-concurrency-multi-threading-with-executorserv

https://www.javatpoint.com/threadgroup-in-java

https://www.digitalocean.com/community/tutorials/threadpoolexecutor-java-thread-pool-example-executorservice
Deadlock in Java

Deadlock in Java is a part of multithreading. Deadlock can occur in a situation


when a thread is waiting for an object lock, that is acquired by another thread and
second thread is waiting for an object lock that is acquired by first thread. Since,
both threads are waiting for each other to release the lock, the condition is called
deadlock.
How to avoid deadlock?

A solution for a problem is found at its roots. In deadlock it is the pattern of


accessing the resources A and B, is the main issue. To solve the issue we will have
to simply re-order the statements where the code is accessing shared resources.
How to Avoid Deadlock in Java?

Deadlocks cannot be completely resolved. But we can avoid them by following basic
rules mentioned below:

1. Avoid Nested Locks: We must avoid giving locks to multiple threads, this is the
main reason for a deadlock condition. It normally happens when you give locks to
multiple threads.
2. Avoid Unnecessary Locks: The locks should be given to the important threads.
Giving locks to the unnecessary threads that cause the deadlock condition.
3. Using Thread Join: A deadlock usually happens when one thread is waiting for the
other to finish. In this case, we can use join with a maximum time that a thread will
take.
Important Points Deadlock :

● If threads are waiting for each other to finish, then the condition is known
as Deadlock.
● Deadlock condition is a complex condition which occurs only in case of
multiple threads.
● Deadlock condition can break our code at run time and can destroy
business logic.
● We should avoid this condition as much as we can.
Java ExecutorService Implementations

The Java ExecutorService is very similar to a thread pool. In fact, the


implementation of the ExecutorService interface present in the
java.util.concurrent package is a thread pool implementation. If you want
to understand how the ExecutorService interface can be implemented
internally, read the above tutorial.

Since ExecutorService is an interface, you need to its implementations in


order to make any use of it. The ExecutorService has the following
implementation in the java.util.concurrent package:

● ThreadPoolExecutor
● ScheduledThreadPoolExecutor
Creating an ExecutorService

How you create an ExecutorService depends on the implementation you use.


However, you can use the Executors factory class to create ExecutorService
instances too. Here are a few examples of creating an ExecutorService:

ExecutorService executorService1 = Executors.newSingleThreadExecutor();


ExecutorService executorService2 = Executors.newFixedThreadPool(10);
ExecutorService executorService3 = Executors.newScheduledThreadPool(10);

You might also like