/* * Created on Dec 30, 2005 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package samplecode; import java.util.*; /** * @author Headley Williamson * Threaded Hash * www.greattastingjava.com code licensed under http://creativecommons.org/licenses/by/2.5/ */ public class HWThreadedHash extends Hashtable { private int maxThreadCount = 10; public HWThreadedHash(int p_maxThreadCount) { maxThreadCount =p_maxThreadCount; } public synchronized void put(String key, HWThread thread) throws Exception { while (getActiveThreadCount() >= maxThreadCount) { Thread.sleep(100); } super.put(key, thread); thread.start(); } private void put(String key, Object x) { } public int getActiveThreadCount() { int count = 0; Enumeration keys = super.keys(); while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); HWThread dbthread = (HWThread) super.get(key); if (!dbthread.isFinished()) { count = count + 1; } } return count; } public Object get(String key) throws Exception { HWThread dbthread = (HWThread) super.get(key); while (!dbthread.isFinished() )) { try { Thread.sleep(100); } catch (Exception ex) { ex.printStackTrace(); } } if (dbthread != null) { if (dbthread.getException() != null) { throw dbthread.getException(); } return dbthread.getData(); } return null; } /** * @return Returns the maxThreadCount. */ public int getMaxThreadCount() { return maxThreadCount; } /** * @param maxThreadCount The maxThreadCount to set. */ public void setMaxThreadCount(int maxThreadCount) { this.maxThreadCount = maxThreadCount; } }