Avatar ·

Java में synchronized कीवर्ड का क्या मतलब है? सरल शब्दों में उदाहरण सहित समझाएं

नमस्ते साथियों! मैं Java में नया हूँ और `synchronized` कीवर्ड क्या करता है, यह समझ नहीं पा रहा हूँ। मैंने डॉक्यूमेंटेशन पढ़ा, लेकिन वह बहुत जटिल लगता है। मेरे पास एक सरल `Counter` क्लास है जो कई threads में काउंटर बढ़ाती है। मैं उम्मीद करता हूँ कि हर thread कुल काउंटर में 1000 जोड़ेगा, लेकिन परिणाम हमेशा अलग आता है: कभी 1987, कभी 2003, जबकि बिल्कुल 2000 होना चाहिए (दो threads, प्रत्येक 1000)। यहाँ मेरा कोड है: ```java public class Counter { private int count = 0; public void increment() { count++; } public int getCount() { return count; } } public class Main { public static void main(String[] args) throws InterruptedException { Counter counter = new Counter(); Thread t1 = new Thread(() -> { for (int i