Warning: Can't use blame annotator:
svn blame failed on trunk/test/tmcsim/client/cadclientgui/data/ThreadDemo.java: ("Can't find a temporary directory: Internal error", 20014)

source: tmcsimulator/trunk/test/tmcsim/client/cadclientgui/data/ThreadDemo.java @ 476

Revision 476, 1.7 KB checked in by jdalbey, 7 years ago (diff)

Add unit tests for CADData.

RevLine 
1
2package tmcsim.client.cadclientgui.data;
3
4import java.util.ArrayList;
5import java.util.Vector;
6import javax.swing.table.DefaultTableModel;
7
8
9/**
10 * A simple demo of how to use threads and "synchronized"
11 *
12 * @author jdalbey
13 */
14public class ThreadDemo
15{
16    static ArrayList<Integer> numbers;
17
18    public static void main(String args[])
19    {
20        System.out.println("Testing threads");
21        PrintFunctor pf=new PrintFunctor();
22        Threadserve ts1=new Threadserve(pf,12,"A");
23        Threadserve ts2=new Threadserve(pf,8,"B");
24        Threadserve ts3=new Threadserve(pf,6,"C");
25    }
26
27static class Threadserve implements Runnable
28{
29        int max;
30        String id;
31        PrintFunctor pf;
32        Thread th;
33        Threadserve(PrintFunctor printer,int x, String id)
34        {
35            this.max = x;
36            this.id = id;
37            pf = printer;
38            th=new Thread(this);
39            th.start();
40        }
41        public void run()
42        {       
43            pf.countdown(max,id);
44        }
45}   
46static class PrintFunctor
47{
48    public PrintFunctor()
49    {
50    }
51    synchronized 
52    void countdown(int max, String id)
53    {
54        numbers = new ArrayList<Integer>();
55        for (int num=0; num <100; num++)
56        {
57            numbers.add(num);
58        }
59        System.out.println("Start" + id);
60        for(int idx=max; idx>0; idx--)
61        {
62            try
63            {
64                // when halfway, pause
65                if(idx==max/2) Thread.sleep(100);
66            }
67            catch(InterruptedException e)
68            { ; }
69            System.out.println(id + ": " + idx + "," + numbers.get(idx));
70        }
71        System.out.println();
72        numbers.clear();
73        System.out.println("Done countdown." + id);
74    }
75}
76}
Note: See TracBrowser for help on using the repository browser.