"नमस्कार, अमीगो! कल हमने मल्टीथ्रेडिंग के लाभों और सुविधाओं पर चर्चा की थी। अब समय आ गया है कि इसके नुकसानों को देखा जाए। और, दुर्भाग्य से, वे छोटे नहीं हैं।"

पहले, हमने एक प्रोग्राम को ऑब्जेक्ट्स के एक सेट के रूप में देखा जो एक दूसरे के तरीकों को कहते हैं। अब सब कुछ थोड़ा और जटिल हो जाता है। एक प्रोग्राम ऑब्जेक्ट्स के एक सेट की तरह अधिक होता है जिसमें कई "छोटे रोबोट" (थ्रेड) होते हैं जो इसके माध्यम से रेंगते हैं और विधियों में निहित कमांड को निष्पादित करते हैं।

यह नई व्याख्या पहले को रद्द नहीं करती है। वे अभी भी वस्तुएं हैं, और वे अभी भी एक दूसरे के तरीकों को कहते हैं। लेकिन हमें यह याद रखना होगा कि कई सूत्र हैं, और प्रत्येक धागा अपना काम या कार्य करता है।

एक कार्यक्रम और अधिक जटिल होता जा रहा है। अलग-अलग धागे अलग-अलग वस्तुओं की स्थिति को उनके द्वारा किए जाने वाले कार्यों के आधार पर बदलते हैं। और वे एक दूसरे के पैर की उंगलियों पर कदम रख सकते हैं।

लेकिन सबसे खराब चीजें जावा मशीन के अंदर ही होती हैं। जैसा कि मैंने पहले ही कहा है, थ्रेड्स की स्पष्ट समानता इस तथ्य से प्राप्त होती है कि प्रोसेसर लगातार एक थ्रेड से दूसरे में स्विच करता है। यह एक थ्रेड पर स्विच करता है, 10 मिलीसेकंड के लिए काम करता है, अगले थ्रेड पर स्विच करता है, 10 मिलीसेकंड के लिए काम करता है, और इसी तरह। और यहाँ समस्या है: ये स्विच सबसे अधिक समय पर हो सकते हैं। इस उदाहरण पर विचार करें:

पहले धागे का कोड दूसरे सूत्र का कोड
System.out.print ("Nick is");
System.out.print ("");
System.out.print ("15");
System.out.print ("");
System.out.print ("years old");
System.out.println ();
System.out.print ("Lena is");
System.out.print ("");
System.out.print ("21");
System.out.print ("");
System.out.print ("years old");
System.out.println ();
हम क्या प्रदर्शित होने की उम्मीद करते हैं
निक 15 साल के हैं
लीना 21 साल की हैं
वास्तविक कोड निष्पादन पहले धागे का कोड दूसरे सूत्र का कोड
System.out.print ("Nick is");
System.out.print ("Lena is");
System.out.print (" ");
System.out.print (" ");
System.out.print ("15");
System.out.print ("21");
System.out.print (" ");
System.out.print (" ");
System.out.print ("years old");
System.out.println ();
System.out.print ("years old");
System.out.println ();
System.out.print ("Nick is");
//other thread is running
//other thread is running
System.out.print (" ");
System.out.print ("15");
//other thread is running
//other thread is running
System.out.print (" ");
System.out.print ("years old");
System.out.println ();
//other thread is running
//other thread is running
//other thread is running
System.out.print ("Lena is");
System.out.print (" ");
//other thread is running
//other thread is running
System.out.print ("21");
System.out.print (" ");
//other thread is running
//other thread is running
//other thread is running
System.out.print ("years old");
System.out.println ();
वास्तव में क्या प्रदर्शित होता है
लीना की  उम्र 15 21 साल है    

और यहाँ एक और उदाहरण है:

कोड विवरण
class MyClass
{
private String name1 = "Ally";
private String name2 = "Lena";
public void swap()
{
String s = name1;
name1 = name2;
name2 = s;
}
}
स्वैप विधि और चर swapके मान ।name1name2

क्या हो सकता है अगर इसे एक ही समय में दो धागे से बुलाया जाए?

वास्तविक कोड निष्पादन पहले धागे का कोड दूसरे सूत्र का कोड
String s1 = name1; //Ally
name1 = name2; //Lena
String s2 = name1; //Lena(!)
name1 = name2; //Lena
name2 = s1; //Ally
name2 = s2; //Lena
String s1 = name1;
name1 = name2;
//other thread is running
//other thread is running
name2 = s1;
//other thread is running
//other thread is running
//other thread is running
String s2 = name1;
name1 = name2;
//other thread is running
name2 = s2;
तल - रेखा
दोनों चरों का मूल्य «लीना» है।
«सहयोगी» वस्तु ने इसे नहीं बनाया। यह खो गया है।

"किसने अनुमान लगाया होगा कि इस तरह के सरल असाइनमेंट ऑपरेशन के साथ इस तरह की त्रुटियां संभव हैं?"

"हाँ, इस समस्या का समाधान है। लेकिन हम इस बारे में थोड़ी देर बाद बात करेंगे - मेरा गला सूख गया है।"