FileDocCategorySizeDatePackage
LoopHandler.javaAPI DocExample1364Thu Mar 11 19:50:48 GMT 2004javathreads.examples.ch15

LoopHandler

public class LoopHandler extends Object implements Runnable

Fields Summary
protected Thread[]
lookupThreads
protected int
startLoop
protected int
endLoop
protected int
curLoop
protected int
numThreads
Constructors Summary
public LoopHandler(int start, int end, int threads)

        startLoop = curLoop = start;
        endLoop = end;
        numThreads = threads;
        lookupThreads = new Thread[numThreads];
    
Methods Summary
public voidloopDoRange(int start, int end)

    
protected synchronized javathreads.examples.ch15.LoopHandler$LoopRangeloopGetRange()

        if (curLoop >= endLoop)
            return null;
        LoopRange ret = new LoopRange();
        ret.start = curLoop;
        curLoop += (endLoop-startLoop)/numThreads+1;
        ret.end = (curLoop<endLoop) ? curLoop : endLoop;
        return ret;
    
public voidloopProcess()

        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();
		lookupThreads[i] = null;
            } catch (InterruptedException iex) {}
        }
    
public voidrun()

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