Page 166 - CITS - CSA - TP (Volume 2) - Hindi
P. 166
कं ूटर सॉ वेयर ए ीके शन - CITS
अ ास 105: िसं ोनाइजेशन के साथ और उसके िबना म ी ेिडंग का परी ण कर (Test multith
readingwith and without synchronization)
उ े
इस अ ास के अंत म आप यह कर सक गे
• िसं ोनाइज़ेशन के साथ और िबना म ी ेिडंग को जान
• िसं ोनाइज़ेशन के साथ और िबना म ी ेिडंग का उपयोग करके JAVA ो ाम डेवलप कर ।
आव कताएं (Requirements)
उपकरण/साम ी (Tools/Materials)
• िवंडोज OS वाला PC/लैपटॉप
• SDK सॉ वेयर
• टे एिडटर (िवजुअल ू िडयो/सबलाइम/नोटपैड)
ि या (Procedure)
//multithreading with and without synchronization
class CounterWithSync {
private int count = 0;
public synchronized void increment() {
for (int i = 0; i < 5; i++) {
int currentValue = count;
System.out.println(Thread.currentThread().getName() + “ - Before Increment: “ + currentValue);
count = currentValue + 1;
System.out.println(Thread.currentThread().getName() + “ - After Increment: “ + count);
}
}
}
class IncrementThreadWithSync extends Thread {
private CounterWithSync counter;
public IncrementThreadWithSync(CounterWithSync counter) {
this.counter = counter;
}
public void run() {
counter.increment();
}
}
public class MultithreadingWithSyncExample {
public static void main(String[] args) {
152

