FileDocCategorySizeDatePackage
PoolLoopHandler.javaAPI DocExample2544Thu Feb 04 16:10:44 GMT 1999None

PoolLoopHandler.java

/*
 *
 * Copyright (c) 1997-1999 Scott Oaks and Henry Wong. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and
 * without fee is hereby granted.
 *
 * This sample source code is provided for example only,
 * on an unsupported, as-is basis. 
 *
 * AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. AUTHOR SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
 * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
 * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
 * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
 * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
 * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
 * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  AUTHOR
 * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
 * HIGH RISK ACTIVITIES.
 */


public class PoolLoopHandler implements Runnable {
	protected class LoopRange {
		public int start, end;
	}
	protected ThreadPool poolThreads;
	protected int startLoop, endLoop, curLoop, numThreads;

	public PoolLoopHandler(int start, int end, int threads) {
		numThreads = threads;
		poolThreads = new ThreadPool(numThreads);
		setRange(start, end);
	}

	public synchronized void setRange(int start, int end) {
		startLoop = start;
		endLoop = end;
		reset();
	}

	public synchronized void reset() {
		curLoop = startLoop;
	}


	protected synchronized LoopRange loopGetRange() {
		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 void loopDoRange(int start, int end) {
	}
 
	public void loopProcess() {
		reset();
		for (int i = 0; i < numThreads; i++) {
			poolThreads.addRequest(this);
		}
		try {
			poolThreads.waitForAll();
		} catch (InterruptedException iex) {
			iex.printStackTrace();
		}
	}
 
	public void run() {
		LoopRange str;
		while ((str = loopGetRange()) != null) {
			loopDoRange(str.start, str.end);
		}
	}
}