
package tmcsim.client.cadclientgui.data;

import java.util.ArrayList;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;


/**
 * A simple demo of how to use threads and "synchronized"
 *
 * @author jdalbey
 */
public class ThreadDemo
{
    static ArrayList<Integer> numbers;

    public static void main(String args[])
    {
        System.out.println("Testing threads");
        PrintFunctor pf=new PrintFunctor();
        Threadserve ts1=new Threadserve(pf,12,"A");
        Threadserve ts2=new Threadserve(pf,8,"B");
        Threadserve ts3=new Threadserve(pf,6,"C");
    }

static class Threadserve implements Runnable
{
        int max;
        String id;
        PrintFunctor pf;
        Thread th;
        Threadserve(PrintFunctor printer,int x, String id)
        {
            this.max = x;
            this.id = id;
            pf = printer;
            th=new Thread(this);
            th.start();
        }
        public void run()
        {       
            pf.countdown(max,id);
        }
}    
static class PrintFunctor
{
    public PrintFunctor()
    {
    }
    synchronized 
    void countdown(int max, String id)
    {
        numbers = new ArrayList<Integer>();
        for (int num=0; num <100; num++)
        {
            numbers.add(num);
        }
        System.out.println("Start" + id);
        for(int idx=max; idx>0; idx--)
        {
            try
            {
                // when halfway, pause
                if(idx==max/2) Thread.sleep(100);
            }
            catch(InterruptedException e)
            { ; }
            System.out.println(id + ": " + idx + "," + numbers.get(idx));
        }
        System.out.println();
        numbers.clear();
        System.out.println("Done countdown." + id);
    }
}
}