CCCPaste

Untitled


public class Main {
    public static void main(String[] args) {
        Brojac b = new Brojac();
        Brojac b2 = new Brojac();
        MyThread t1 = new MyThread(1, b);
        MyThread t2 = new MyThread(2, b2);
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        }
        catch (InterruptedException ie) {
            System.out.println(ie);
        }
       System.out.println(b.getBr());
        System.out.print(b2.getBr());
    }
}

class MyThread extends Thread {
    private int id;
    Brojac b;
    public MyThread(int _id, Brojac _b) {
        id = _id;
        b = _b;
    }

    @Override
    public void run() {
        for(int i = 0; i < 10; ++i) {
//            System.out.println("Thread " + id + " " + i);
            b.safe_zgolemi_brojac();
            // zemi ja vrednosta na b
            // zgolemi ja vrednosta T1, T2
            // vrati ja vrednost
        }
    }
}

class Brojac {
    private static int br = 0;

    public static void unsafe_zgolemi_brojac() {
        br++; // no atomicity
        try {
            Thread.sleep(10);
        }
        catch (InterruptedException ie) {
            System.out.println(ie);
        }
    }
    public synchronized void safe_zgolemi_brojac() {
        br++; // atomocity
    }

    public static int getBr() {
        return br;
    }

}