Skip to content

12.2 Thread Methods & Synchronization

class Counter {
    private int count = 0;

    // synchronized से एक समय में सिर्फ एक ही थ्रेड अंदर आ सकता है (Race Condition Safe)
    public synchronized void increment() {
        count++;
    }

    public int getCount() { return count; }
}

public class SyncDemo {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) counter.increment();
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) counter.increment();
        });

        t1.start();
        t2.start();

        t1.join(); // t1 के खत्म होने का इंतज़ार करें
        t2.join(); // t2 के खत्म होने का इंतज़ार करें

        System.out.println("Final Count (Always 2000): " + counter.getCount());
    }
}

🧭 Navigation

Last updated on