Sahil-Pattni
11/16/2016 - 7:44 PM

Divider LAB12

Divider LAB12

/**
 * Created by Sahil Pattni on 11/17/16
 */
public class Divider implements Runnable
{
    public static int counter = 0;
    private static Object lock = new Object();
    public static int start;
    public static int end;

    public Divider(int start, int end)
    {
        this.start = start;
        this.end = end;
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(new Divider(1,1000));
        Thread t2 = new Thread(new Divider(1001, 2000));
        Thread t3 =  new Thread(new Divider(2001, 3000));

        t1.start();
        t2.start();
        t3.start();

        try
        {
            t1.join();
            t2.join();
            t3.join();
        }
        catch (InterruptedException e)
        {
            System.out.println("Big Problem");
        }
        System.out.print(counter);
    }

    public void run()
    {
        for (int i = start; i <= end; i++)
        {
            if(i % 7 == 0)
            {
                synchronized (lock)
                {
                    this.counter++;
                }
            }
        }
    }
}