Chapter 21 - Exercise 12

(3) Repair AtomicityTest.java using the synchronized keyword. Can you demonstrate that it is now correct?


description: > (1) Repair SerialNumberChecker.java using the synchronized keyword. Can you demonstrate that it is now correct? —



package com.ciaoshen.thinkinjava.chapter21;
import java.util.concurrent.*;

public class Exercise12 implements Runnable{
    private volatile int i=0;
    private int duration;
    public Exercise12(int duration){
        this.duration=duration;
    }
    public synchronized void f3(){
        i++;i++;i++;
    }
    public void run(){
        long stop=System.currentTimeMillis()+duration;
        while(System.currentTimeMillis() < stop){
            f3();
        }
        Thread.yield();
    }
    public int getValue(){return i;}

    public class Checker implements Runnable{
        public void check(){
            int value;
            synchronized(Exercise12.this){
                value=getValue();
            }
            if(value%3!=0){
                System.out.println(value);
            }
        }
        public void run(){
            while(true){
                check();
                Thread.yield();
            }
        }
    }

    public static void main(String[] args){
        ExecutorService mainService=Executors.newCachedThreadPool();
        ExecutorService daemonService=Executors.newCachedThreadPool(new ThreadFactory(){
            public Thread newThread(Runnable r){
                Thread th=new Thread(r);
                th.setDaemon(true);
                return th;
            }
        });
        Exercise12 task=new Exercise12(1000);
        Checker checker=task.new Checker();

        mainService.execute(task);
        daemonService.execute(checker);

        mainService.shutdown();
        daemonService.shutdown();
    }
}



Need Java Developers? Hire Me »

I know Java. Talk is cheap, watch my code.

I live in CANADA now.