Multithreading in Java (Part3)
Multithreading in Java (Part3)
(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
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
● ThreadPoolExecutor
● ScheduledThreadPoolExecutor
Creating an ExecutorService