1 /*****************************************************************************/ 2 /* Software Testing Automation Framework (STAF) */ 3 /* (C) Copyright IBM Corp. 2002 */ 4 /* */ 5 /* This software is licensed under the Eclipse Public License (EPL) V1.0. */ 6 /*****************************************************************************/ 7 8 package com.ibm.staf.service.stax; 9 import java.util.LinkedList; 10 11 // XXX: This thread should probably have a list of all STAXThreads being run as 12 // well as those on the queue. This would allow us to make sure that we don't 13 // try to run the same STAXThread on two real threads at the same time, which 14 // I think actually happens now. 15 16 public class STAXThreadQueue 17 { STAXThreadQueue(int numThreads)18 public STAXThreadQueue(int numThreads) 19 { 20 for (int i = 0; i < numThreads; ++i) 21 { 22 QueueThread thread = new QueueThread(); 23 fThreads.add(thread); 24 thread.start(); 25 } 26 } 27 add(STAXThread thread)28 public void add(STAXThread thread) 29 { 30 synchronized (fQueue) 31 { 32 fQueue.addLast(thread); 33 fQueue.notify(); 34 } 35 } 36 37 class QueueThread extends Thread 38 { run()39 public void run() 40 { 41 STAXThread thread = null; 42 43 for (;;) 44 { 45 try 46 { 47 synchronized (fQueue) 48 { 49 while (fQueue.size() == 0) fQueue.wait(); 50 51 thread = fQueue.removeFirst(); 52 } 53 54 thread.execute(); 55 } 56 catch (InterruptedException e) 57 { 58 // XXX: What to do? What does this really mean? 59 } 60 } 61 } 62 } 63 64 LinkedList<STAXThread> fQueue = new LinkedList<STAXThread>(); 65 LinkedList<QueueThread> fThreads = new LinkedList<QueueThread>(); 66 } 67