Critical Sections in Java

Critical Sections in Java

A method can be made a critical section in Java by prefixing its definition with the keyword synchronized. The corrected code for the Counter class becomes:

class Counter {

int value_=0;

public synchronized void increment() {

int temp = value_; //read

Simulate.interrupt();

++temp; //add1

value_=temp; //write

}

}

Run the demonstration program -

Demo

Previous slide Next slide Back to the first slide View Graphic Version