Java Synchronization जावा में सिंक्रोनाइजेशन

Synchronization in Java is a technique used to control the access of multiple threads to a shared resource. When multiple threads try to access the same method or resource simultaneously, it may lead to inconsistency or unexpected behavior. Synchronization ensures that only one thread can access the resource at a time.

जावा में सिंक्रोनाइजेशन एक ऐसी तकनीक है जिसका उपयोग तब किया जाता है जब एक से अधिक थ्रेड किसी साझा संसाधन (resource) को एक ही समय पर एक्सेस करने की कोशिश करते हैं। यह स्थिति डेटा इनकंसिस्टेंसी या प्रोग्राम एरर का कारण बन सकती है। सिंक्रोनाइजेशन के द्वारा हम सुनिश्चित करते हैं कि एक समय में केवल एक ही थ्रेड उस संसाधन को एक्सेस करे।


🧵 Why Synchronization is Needed? (सिंक्रोनाइजेशन क्यों आवश्यक है?)

English:

  • Prevents thread interference थ्रेड हस्तक्षेप को रोकने के लिए

  • Ensures data consistency डेटा की स्थिरता बनाए रखने के लिए

  • Useful in banking apps, inventory systems, etc. बैंकिंग या इन्वेंट्री जैसे एप्लिकेशन में आवश्यक


🛠️ Ways to Synchronize in Java (जावा में सिंक्रोनाइजेशन के तरीके)

1️⃣ Synchronized Method (सिंक्रोनाइज्ड मेथड)

You can synchronize an entire method by adding the synchronized keyword to its declaration. This locks the resource for that thread.

आप किसी मेथड को पूरी तरह से synchronized बना सकते हैं। इसके लिए synchronized कीवर्ड को मेथड डिक्लेरेशन में जोड़ा जाता है। इससे वह मेथड उस थ्रेड के लिए लॉक हो जाता है।

Syntax:

synchronized return_type methodName() {
// critical section
}

2️⃣ Synchronized Block (सिंक्रोनाइज्ड ब्लॉक)

If only part of the method needs to be synchronized, you can use a synchronized block to lock only that section.

यदि पूरे मेथड को synchronized बनाना आवश्यक न हो तो केवल उस भाग को लॉक किया जा सकता है जहाँ साझा संसाधन उपयोग हो रहा हो।

Syntax:

synchronized(object_reference) {
// critical code
}

🧪 Example: Synchronized Method

Below is a Java program that uses a synchronized method to print a multiplication table safely when accessed by two threads:

English:
This example shows that even if two threads call the same method on the same object, the output remains consistent.

Hindi:
यह उदाहरण दर्शाता है कि दो थ्रेड्स यदि एक ही ऑब्जेक्ट की मेथड को कॉल करें, तब भी आउटपुट बिना टकराव के आता है।

// Java Program to demonstrate synchronized method
class Table {
synchronized void printTable(int n) {
for (int i = 1; i <= 10; i++) {
System.out.println(n * i);
try {
Thread.sleep(400);
} catch (Exception e) {
System.out.println(e);
}
}
}
}
public class TestSynchronization {
public static void main(String args[]) {
final Table obj = new Table(); // shared object
Thread t1 = new Thread() {
public void run() {
obj.printTable(5);
}
};
Thread t2 = new Thread() {
public void run() {
obj.printTable(100);
}
};
t1.start();
t2.start();
}
}

Output (आउटपुट):

5
10
15
...
50
100
200
300
...
1000

📝 Output always comes in a sequence – either full 5-table or full 100-table first, never mixed — thanks to synchronization.


🔐 Benefits of Synchronization (सिंक्रोनाइजेशन के लाभ)

✅ Thread-safe resource access थ्रेड-सेफ संसाधन एक्सेस
✅ Avoids race conditions रेस कंडीशन से सुरक्षा
✅ Suitable for multi-user systems मल्टी-यूज़र सिस्टम में उपयोगी
✅ Reduces inconsistent state errors डेटा असंगतता से बचाव



⚠️ Caution While Using Synchronization (सावधानी)

English:
Overusing synchronization may cause: सिंक्रोनाइजेशन का अत्यधिक उपयोग कर सकते हैं:

  • Performance degradation परफॉर्मेंस में कमी

  • Deadlocks if not handled carefully डेडलॉक की संभावना


🔚 Conclusion (निष्कर्ष)

Synchronization is a vital part of multithreaded programming in Java. It helps maintain consistency and correctness in concurrent applications by allowing only one thread to access a resource at a time.

सिंक्रोनाइजेशन जावा की मल्टीथ्रेडिंग का एक अनिवार्य हिस्सा है। यह एक समय में केवल एक थ्रेड को साझा संसाधन तक पहुंच प्रदान कर प्रोग्राम को सुरक्षित और सटीक बनाता है।

Comments

Popular posts from this blog

What is a Web Browser? वेब ब्राउज़र क्या है?

Java's Support System जावा का सहयोगी तंत्र

The Internet and Java इंटरनेट और जावा का सम्बन्ध