Java Help
Sunday, 8 March 2020
Deadlock in Java and Solution
Deadlock in Java and Solution
Please find below the program for Deadlock in Java.
class A { } class B { } public class HelloWorld { final static A a = new A(); final static B b = new B(); public static void main(String[] args) { // Thread 1 Runnable thrd1 = () -> { // Resource a synchronized (a) { try { Thread.sleep(100); } catch (InterruptedException ex) { } // Resource b synchronized (b) { System.out.println("Thread 1"); } } }; // Thread 2 Runnable thrd2 = () -> { // hold Resorce b by thread1 synchronized (b) { synchronized (a) { System.out.println("Thread 2"); } } }; // threads start new Thread(thrd1).start(); new Thread(thrd2).start(); // Solution : To Avoid Deadlock, replace sequence of resources } }
Solution: To Avoid Deadlock, replace the sequence of resources
Program Link
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment