package edu.calpoly.cpe205.fetter; import javax.swing.*; import java.util.*; import java.io.*; import java.lang.reflect.*; import java.lang.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; /** * This class maintains a list of idle ReturnThreads that can be returned to * the developer to be used to perform a specific task. * @see ReturnThread */ // Author: Wes Strickland // Version History // Nov 19, 2000 - comments/pseudocode added // Nov 30, 2000 - class description changed // Feb 8, 2001 - comments added and braces moved for Stage Two release // Feb 24, 2001 - (Phillip Hansen) coded freeThread(ReturnThread), // getFreeThread(), ThreadFreeList(), getThreadFreeList() public class ThreadFreeList { /** * This constructor is called once to instantiate only one instance of * ThreadFreeList * Pre-conditions: none * Post-conditions: one and only instance of ThreadFreeList is constructed */ private ThreadFreeList() { // SET freeThreads to NEW collection // assigns new collection to freeThreads freeThreads = new LinkedList(); } /** * This method returns the only instance of ThreadFreeList * Pre-conditions: none * Post-conditions: theThreadFreeList is returned */ public static ThreadFreeList getThreadFreeList() { // RETURN theThreadFreeList return theThreadFreeList; } /** * Returns a thread that is ready for execution * Pre-conditions: none * Post-conditions: Next thread ready for execution is returned */ public ReturnThread getFreeThread() { ReturnThread freeThread; // the freeThread to be returned // CALL isEmpty of collection // IF freeThreads is empty THEN // RETURN new returnThread // ELSE // CALL iterator of freeThreads // CALL next of iterator // CALL remove of iterator with nextitem // RETURN nextitem /* The following block of code causes a defect because reusing threads doesn't seem to work. You can't call start() on the same thread twice. if (freeThreads.isEmpty()) { freeThread = new ReturnThread(); } else { Iterator itr = freeThreads.iterator(); freeThread = (ReturnThread)itr.next(); itr.remove(); }*/ freeThread = new ReturnThread(); return freeThread; } /** * Adds a vacationing thread to thelist of FreeThreads * Pre-conditions: none * Post-conditions: vacationingThread added to FreeThreads * @param vacationingThread a return thread that has completed execution */ public void freeThread(Thread vacationingThread) { // CALL add of freeThreads with vacationingThread freeThreads.add(vacationingThread); } private static ThreadFreeList theThreadFreeList = new ThreadFreeList(); //Inner copy of list /** *@link aggregation * @associates <{ReturnThread}> */ private java.util.Collection freeThreads; //Local private copy of collection of free threads }