FileDocCategorySizeDatePackage
SinTable.javaAPI DocExample2042Thu Apr 22 08:15:38 BST 2004javathreads.examples.ch15.example2

SinTable

public class SinTable extends Object implements Runnable

Fields Summary
private float[]
lookupValues
private Thread[]
lookupThreads
private int
startLoop
private int
endLoop
private int
curLoop
private int
numThreads
Constructors Summary
public SinTable()

        lookupValues = new float [360 * 100];
        lookupThreads = new Thread[12];
        startLoop = curLoop = 0;
        endLoop = (360 * 100);
        numThreads = 12;
    
Methods Summary
public float[]getValues()

        for (int i = 0; i < numThreads; i++) {
            lookupThreads[i] = new Thread(this);
            lookupThreads[i].start();
        }
        for (int i = 0; i < numThreads; i++) {
            try {
                lookupThreads[i].join();
            } catch (InterruptedException iex) {}
        }
        return lookupValues;
    
private voidloopDoRange(int start, int end)

        for (int i = start; i < end; i += 1) {
            float sinValue = (float)Math.sin((i % 360)*Math.PI/180.0);
            lookupValues[i] = sinValue * (float)i / 180.0f;
        }
    
private synchronized javathreads.examples.ch15.example2.SinTable$SinTableRangeloopGetRange()

        if (curLoop >= endLoop)
            return null;
        SinTableRange ret = new SinTableRange();
        ret.start = curLoop;
        curLoop += (endLoop-startLoop)/numThreads+1;
        ret.end = (curLoop<endLoop)?curLoop:endLoop;
        return ret;
    
public static voidmain(java.lang.String[] args)

        System.out.println("Starting Example 2 (Threaded Example)");

        SinTable st = new SinTable();
        float results[] = st.getValues();

        System.out.println("Results: "+ results[0]+ ", "+
                      results[1]+ ", "+ results[2]+ ", "+ "...");
        System.out.println("Done");
    
public voidrun()

        SinTableRange str;
        while ((str = loopGetRange()) != null) {
            loopDoRange(str.start, str.end);
        }