├── .gitignore ├── Example1 ├── .classpath ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ └── org.eclipse.jdt.core.prefs ├── bin │ ├── com │ │ └── masai │ │ │ ├── Deadlock1.class │ │ │ └── Demo.class │ └── module-info.class └── src │ ├── com │ └── masai │ │ ├── Deadlock1.java │ │ └── Demo.java │ └── module-info.java ├── Example2 ├── .classpath ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ └── org.eclipse.jdt.core.prefs ├── bin │ ├── com │ │ └── masai │ │ │ └── Demo.class │ └── module-info.class └── src │ ├── com │ └── masai │ │ └── Demo.java │ └── module-info.java └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /.metadata/ 2 | -------------------------------------------------------------------------------- /Example1/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Example1/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Example1 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Example1/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding/=UTF-8 3 | -------------------------------------------------------------------------------- /Example1/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=17 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled 11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 12 | org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning 13 | org.eclipse.jdt.core.compiler.release=enabled 14 | org.eclipse.jdt.core.compiler.source=17 15 | -------------------------------------------------------------------------------- /Example1/bin/com/masai/Deadlock1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shubh2-0/Deadlock-In-Java/c60c48e8312668c238a8111316196354f2041ace/Example1/bin/com/masai/Deadlock1.class -------------------------------------------------------------------------------- /Example1/bin/com/masai/Demo.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shubh2-0/Deadlock-In-Java/c60c48e8312668c238a8111316196354f2041ace/Example1/bin/com/masai/Demo.class -------------------------------------------------------------------------------- /Example1/bin/module-info.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shubh2-0/Deadlock-In-Java/c60c48e8312668c238a8111316196354f2041ace/Example1/bin/module-info.class -------------------------------------------------------------------------------- /Example1/src/com/masai/Deadlock1.java: -------------------------------------------------------------------------------- 1 | package com.masai; 2 | 3 | public class Deadlock1 { 4 | 5 | 6 | public void fun(); 7 | 8 | } 9 | -------------------------------------------------------------------------------- /Example1/src/com/masai/Demo.java: -------------------------------------------------------------------------------- 1 | package com.masai; 2 | 3 | public class Demo { 4 | 5 | public static void main(String[] args) { 6 | 7 | 8 | final String resource1 = "Printer"; 9 | final String resource2 = "Scanner"; 10 | 11 | Runnable ru1 = () -> { 12 | 13 | synchronized (resource1) { 14 | 15 | System.out.println(Thread.currentThread().getName()+": locked "+resource1); 16 | 17 | try { 18 | 19 | Thread.sleep(100); 20 | 21 | } catch (Exception e) {} 22 | 23 | synchronized (resource2) { 24 | 25 | System.out.println(Thread.currentThread().getName()+": locked "+resource2); 26 | 27 | } 28 | 29 | 30 | 31 | } 32 | 33 | 34 | 35 | }; 36 | 37 | 38 | 39 | Runnable ru2 = () ->{ 40 | 41 | synchronized (resource2) { 42 | 43 | System.out.println(Thread.currentThread().getName()+": locked "+resource2); 44 | 45 | try { 46 | 47 | Thread.sleep(100); 48 | 49 | } catch (Exception e) {} 50 | 51 | 52 | synchronized (resource1) { 53 | 54 | System.out.println(Thread.currentThread().getName()+": locked "+resource1); 55 | } 56 | 57 | 58 | } 59 | 60 | 61 | 62 | 63 | 64 | }; 65 | 66 | Thread thread1 = new Thread(ru1); 67 | thread1.setName("Desktop"); 68 | Thread thread2 = new Thread(ru2); 69 | thread2.setName("Laptop"); 70 | 71 | 72 | thread1.start(); 73 | thread2.start(); 74 | 75 | 76 | 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /Example1/src/module-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | /** 5 | * @author shubh 6 | * 7 | */ 8 | module Example1 { 9 | } -------------------------------------------------------------------------------- /Example2/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Example2/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Example2 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Example2/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding/=UTF-8 3 | -------------------------------------------------------------------------------- /Example2/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=17 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled 11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 12 | org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning 13 | org.eclipse.jdt.core.compiler.release=enabled 14 | org.eclipse.jdt.core.compiler.source=17 15 | -------------------------------------------------------------------------------- /Example2/bin/com/masai/Demo.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shubh2-0/Deadlock-In-Java/c60c48e8312668c238a8111316196354f2041ace/Example2/bin/com/masai/Demo.class -------------------------------------------------------------------------------- /Example2/bin/module-info.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shubh2-0/Deadlock-In-Java/c60c48e8312668c238a8111316196354f2041ace/Example2/bin/module-info.class -------------------------------------------------------------------------------- /Example2/src/com/masai/Demo.java: -------------------------------------------------------------------------------- 1 | package com.masai; 2 | 3 | import java.io.File; 4 | import java.io.FileNotFoundException; 5 | import java.io.PrintStream; 6 | import java.io.PrintWriter; 7 | import java.util.ArrayList; 8 | import java.util.Arrays; 9 | import java.util.List; 10 | 11 | public class Demo { 12 | 13 | public static void main(String[] args) throws FileNotFoundException { 14 | 15 | String resource1 = "Water"; 16 | String resource2 = "Spices"; 17 | 18 | Runnable run1 = () -> { 19 | 20 | synchronized (resource1) { 21 | 22 | System.out.println(Thread.currentThread().getName()+" have "+resource1); 23 | 24 | try { 25 | 26 | resource1.wait(1000); 27 | 28 | } catch (Exception e) { 29 | // TODO Auto-generated catch block 30 | e.printStackTrace(); 31 | } 32 | 33 | synchronized (resource2) { 34 | 35 | System.out.println(Thread.currentThread().getName()+" have "+resource2); 36 | 37 | } 38 | 39 | 40 | } 41 | 42 | }; 43 | 44 | Runnable ru2 = () -> { 45 | 46 | synchronized (resource2) { 47 | 48 | 49 | System.out.println(Thread.currentThread().getName()+" have "+resource2); 50 | 51 | 52 | try { 53 | resource2.wait(1000); 54 | } catch (InterruptedException e) { 55 | // TODO Auto-generated cathch block 56 | e.printStackTrace(); 57 | } 58 | 59 | synchronized (resource1) { 60 | 61 | System.out.println(Thread.currentThread().getName()+" have "+resource1); 62 | 63 | resource1.notify(); 64 | } 65 | 66 | 67 | 68 | 69 | } 70 | 71 | }; 72 | 73 | 74 | 75 | 76 | Thread thread1 = new Thread(run1); 77 | thread1.setName("Ram"); 78 | Thread thread2 = new Thread(ru2); 79 | thread2.setName("Shiv"); 80 | 81 | thread1.start(); 82 | thread2.start(); 83 | // 84 | // PrintWriter r = new PrintWriter("abc.txt"); 85 | // 86 | // r.write("helloj"); 87 | // r.write("helloj"); 88 | // r.write("helloj"); 89 | // r.write("helloj"); 90 | // 91 | // r.flush(); 92 | 93 | // List list = Arrays.asList(1,2,3,4,5,6,7,8,9,10); 94 | // 95 | // File file = new File("absh.txt"); 96 | // PrintStream pw = null; 97 | // try { 98 | // pw = new PrintStream(file); 99 | // } catch (FileNotFoundException e) { 100 | // e.printStackTrace(); 101 | // } 102 | // 103 | // pw.println("Anuj 56.25 77.58"); 104 | // pw.println("Bharat 66.25 57.58"); 105 | // pw.println("Chaman 70.25 66.74"); 106 | // pw.println("Dhanush 58.25 95.74"); 107 | // pw.println("Garv 58.62 95.74"); 108 | // 109 | // int count = list.stream().reduce(0, (e,ee) -> e+ee); 110 | // 111 | // System.out.println(count); 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | } 123 | 124 | 125 | 126 | 127 | 128 | 129 | } 130 | -------------------------------------------------------------------------------- /Example2/src/module-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | /** 5 | * @author shubh 6 | * 7 | */ 8 | module Example2 { 9 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DEAD LOCK 2 | 3 | ![image](https://github.com/Shubh2-0/Deadlock-In-Java/assets/112773220/7b33551f-8752-4e4b-924a-8c3e72d337d8) 4 | 5 | 6 | 🔒 **A GitHub Repository showcasing projects on Deadlock scenarios** 7 | 8 | This repository contains two projects that demonstrate the concept of deadlock in different scenarios. 9 | 10 | 11 | 12 | ## Projects 13 | 14 | ### Example-1: Printer Scanner 15 | 16 | 🖨️🔎 17 | 18 | This project showcases a simulation of a deadlock scenario between a printer and a scanner. The code demonstrates how two processes can deadlock when they both require exclusive access to shared resources, resulting in a deadlock state. Through this example, you will gain a better understanding of how deadlock can occur and the importance of resource allocation and synchronization. 19 | 20 | ### Example-2: Water Spices 21 | 22 | 💦🌶️ 23 | 24 | The second project focuses on a deadlock scenario related to water and spices. It presents a practical example where multiple threads attempt to access resources concurrently, leading to a potential deadlock situation. By exploring this example, you will learn about the importance of resource management and techniques to avoid or resolve deadlocks. 25 | 26 | ## Getting Started 27 | 28 | To get started with these projects, follow the instructions below: 29 | 30 | 1. Clone this repository: `git clone https://github.com/Shubh2-0/Deadlock-In-Java.git` 31 | 2. Navigate to the respective project directory: `cd example-1` or `cd example-2` 32 | 3. Follow the project-specific instructions provided in the README of each project directory to run the code and observe the deadlock scenarios. 33 | 34 | ## Contributing 35 | 36 | If you would like to contribute to this repository, feel free to submit pull requests. Contributions that enhance the understanding of deadlock scenarios or provide additional examples are highly appreciated. 37 | 38 | ## 📬 Contact 39 | 40 | If you want to contact me, you can reach me through below handles. 41 | 42 |

43 | linkedin  44 | mail-me  45 | whatsapp-me  46 |

47 | 48 |
49 | 50 |
51 | Happy coding! 😄🚀 52 |
53 | 54 | 55 | 56 | --------------------------------------------------------------------------------