class Count extends Thread {
// number of increments per thread
static int N = 1000;
Copyright By PowCoder代写 加微信 powcoder
// shared data
static volatile int counter = 0;
// protocol variables
public void run() {
for (int i = 0; i < N; i++) {
// non-critical section
// pre-protocol section
// critical section
temp = counter;
counter = temp + 1;
// post-protocol section
public static void main(String[] args) {
Count p = new Count();
Count q = new Count();
p.start();
q.start();
try { p.join(); q.join(); }
catch (InterruptedException e) { }
System.out.println("The final value of the counter is " + counter);
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com