Deadlock - Essential Java Classes
Maybe your like
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.See Dev.java for updated tutorials taking advantage of the latest releases.See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
DeadlockDeadlock describes a situation where two or more threads are blocked forever, waiting for each other. Here's an example.
Alphonse and Gaston are friends, and great believers in courtesy. A strict rule of courtesy is that when you bow to a friend, you must remain bowed until your friend has a chance to return the bow. Unfortunately, this rule does not account for the possibility that two friends might bow to each other at the same time. This example application, Deadlock, models this possibility:
public class Deadlock { static class Friend { private final String name; public Friend(String name) { this.name = name; } public String getName() { return this.name; } public synchronized void bow(Friend bower) { System.out.format("%s: %s" + " has bowed to me!%n", this.name, bower.getName()); bower.bowBack(this); } public synchronized void bowBack(Friend bower) { System.out.format("%s: %s" + " has bowed back to me!%n", this.name, bower.getName()); } } public static void main(String[] args) { final Friend alphonse = new Friend("Alphonse"); final Friend gaston = new Friend("Gaston"); new Thread(new Runnable() { public void run() { alphonse.bow(gaston); } }).start(); new Thread(new Runnable() { public void run() { gaston.bow(alphonse); } }).start(); } }When Deadlock runs, it's extremely likely that both threads will block when they attempt to invoke bowBack. Neither block will ever end, because each thread is waiting for the other to exit bow.
« Previous • Trail • Next »About Oracle | Contact Us | Legal Notices | Terms of Use | Your Privacy Rights
Copyright © 1995, 2024 Oracle and/or its affiliates. All rights reserved.
Previous page: Liveness Next page: Starvation and LivelockTag » What Is A Deadlock In Java
-
Deadlock In Java Example - DigitalOcean
-
Deadlock In Java - Javatpoint
-
How To Handle Deadlock In Java? - Edureka
-
Java - Thread Deadlock - Tutorialspoint
-
Deadlock In Java Multithreading - GeeksforGeeks
-
Java Thread Deadlock And Livelock - Baeldung
-
How To Avoid Deadlock In Java - TechVidvan
-
Deadlock In Java - Linux Hint
-
Deadlock In Java Multi-Threading - Medium
-
How To Handle Deadlock In Java? | Edureka - Medium
-
What Is Deadlock In Java Threads? - W3schools
-
Deadlock In Java | Realtime Example - Scientech Easy
-
Deadlock In Java - Adservio
-
Java Deadlock Example And Solution - HowToDoInJava