मैं बहु सूत्रण के लिए नया हूँ में होता है, और मैं इस उदाहरण में आए:कारण समझकर गतिरोध इस कार्यान्वयन
Thread 1: Holding lock 1...
Thread 2: Holding lock 2...
Thread 1: Waiting for lock 2...
Thread 2: Waiting for lock 1...
यानी, वहाँ एक गतिरोध है:
public class TestThread {
public static Object Lock1 = new Object();
public static Object Lock2 = new Object();
public static void main(String args[]) {
ThreadDemo1 T1 = new ThreadDemo1();
ThreadDemo2 T2 = new ThreadDemo2();
T1.start();
T2.start();
}
private static class ThreadDemo1 extends Thread {
public void run() {
synchronized (Lock1) {
System.out.println("Thread 1: Holding lock 1...");
try { Thread.sleep(10); }
catch (InterruptedException e) {}
System.out.println("Thread 1: Waiting for lock 2...");
synchronized (Lock2) {
System.out.println("Thread 1: Holding lock 1 & 2...");
}
}
}
}
private static class ThreadDemo2 extends Thread {
public void run() {
synchronized (Lock2) {
System.out.println("Thread 2: Holding lock 2...");
try { Thread.sleep(10); }
catch (InterruptedException e) {}
System.out.println("Thread 2: Waiting for lock 1...");
synchronized (Lock1) {
System.out.println("Thread 2: Holding lock 1 & 2...");
}
}
}
}
}
यह निम्न नमूना उत्पादन का कारण बनता है । कर सकते हैं
Thread 1: Holding lock 1...
Thread 1: Waiting for lock 2...
Thread 1: Holding lock 1 & 2...
Thread 2: Holding lock 1...
Thread 2: Waiting for lock 2...
Thread 2: Holding lock 1 & 2...
: हालांकि, अगर हम दूसरे सूत्र में प्राप्त ताले का क्रम बदल इतना है कि यह इस तरह अब लग रहा है:
public class TestThread {
public static Object Lock1 = new Object();
public static Object Lock2 = new Object();
public static void main(String args[]) {
ThreadDemo1 T1 = new ThreadDemo1();
ThreadDemo2 T2 = new ThreadDemo2();
T1.start();
T2.start();
}
private static class ThreadDemo1 extends Thread {
public void run() {
synchronized (Lock1) {
System.out.println("Thread 1: Holding lock 1...");
try { Thread.sleep(10); }
catch (InterruptedException e) {}
System.out.println("Thread 1: Waiting for lock 2...");
synchronized (Lock2) {
System.out.println("Thread 1: Holding lock 1 & 2...");
}
}
}
}
private static class ThreadDemo2 extends Thread {
public void run() {
synchronized (Lock1) {
System.out.println("Thread 2: Holding lock 1...");
try { Thread.sleep(10); }
catch (InterruptedException e) {}
System.out.println("Thread 2: Waiting for lock 2...");
synchronized (Lock2) {
System.out.println("Thread 2: Holding lock 1 & 2...");
}
}
}
}
}
यह उम्मीद काम करता है के रूप में, और एक नमूना उत्पादन इस तरह दिखता है कोई मुझे बताता है कि पहले व्यक्ति में क्या हो रहा है जो डेडलॉक पैदा कर रहा है, और दूसरे कोड में परिवर्तन इसे ठीक क्यों करता है?
धन्यवाद। अब मैं पहले मामले को समझ सकता हूं। लेकिन दूसरा मामला अभी भी थोड़ा आलसी है। तो थ्रेड 2 पहले लॉक के लिए इंतजार कर रहा है। जैसे ही इसे थ्रेड 1 द्वारा जारी किया जाता है, तब क्या होता है? – SexyBeast
@AttitudeMonger थ्रेड 2 इसे प्राप्त करता है और सिंक्रनाइज़ किए गए ब्लॉक के तहत पहले कथन का निष्पादन शुरू करता है। – Maroun
जो 'System.out.println है ("थ्रेड 2: होल्डिंग लॉक 1 और 2 ..."); '? और यह लॉक 2 जारी करता है, जिसे थ्रेड 1 द्वारा अधिग्रहित किया जाता है, और यह 'System.out.println प्रिंट करता है ("थ्रेड 1: होल्डिंग लॉक 1 और 2 ..."); '? – SexyBeast