📄 staxparallelaction.java
字号:
/*****************************************************************************//* Software Testing Automation Framework (STAF) *//* (C) Copyright IBM Corp. 2002 *//* *//* This software is licensed under the Common Public License (CPL) V1.0. *//*****************************************************************************/package com.ibm.staf.service.stax;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;public class STAXParallelAction implements STAXAction, STAXThreadCompleteListener{ static final int INIT = 0; static final int WAIT_THREADS = 1; static final int THREADS_COMPLETE = 2; static final int COMPLETE = 3; static final String INIT_STRING = "INIT"; static final String WAIT_THREADS_STRING = "WAIT_THREADS"; static final String THREADS_COMPLETE_STRING = "THREADS_COMPLETE"; static final String COMPLETE_STRING = "COMPLETE"; static final String STATE_UNKNOWN_STRING = "UNKNOWN"; private STAXParallelAction() { /* Do Nothing */ } public STAXParallelAction(ArrayList actionList) { fActionList = actionList; } public String getStateAsString() { switch (fState) { case INIT: return INIT_STRING; case WAIT_THREADS: return WAIT_THREADS_STRING; case THREADS_COMPLETE: return THREADS_COMPLETE_STRING; case COMPLETE: return COMPLETE_STRING; default: return STATE_UNKNOWN_STRING; } } public String getInfo() { return fActionList.size() + " "; } public String getDetails() { return "ActionListSize:" + fActionList.size() + ";State:" + getStateAsString() + ";ActionList:" + fActionList + ";HoldThreadCondition:" + fHoldCondition + ";HardHoldThreadCondition:" + fHardHoldCondition + ";ThreadMap:" + fThreadMap; } public void execute(STAXThread thread) { if (fState == INIT) { // Setup all the new threads. We need to synchronize on the // Thread map so that all the threads are added before the // threadComplete() method does its checks. synchronized (fThreadMap) { for (int i = 0; i < fActionList.size(); ++i) { STAXThread childThread = thread.createChildThread(); STAXAction childThreadAction = (STAXAction)fActionList.get(i); fThreadMap.put(new Integer(childThread.getThreadNumber()), childThread); childThread.addCompletionNotifiee(this); childThread.pushAction(childThreadAction.cloneAction()); childThread.schedule(); } fState = WAIT_THREADS; thread.addCondition(fHoldCondition); } } else if (fState == THREADS_COMPLETE) { // XXX: Maybe set some variables? fState = COMPLETE; thread.popAction(); } } public void handleCondition(STAXThread thread, STAXCondition cond) { synchronized (fThreadMap) { if (!fThreadMap.isEmpty()) { // Add a hard hold, so we can wait until our children terminate. // This is removed by threadComplete(). thread.addCondition(fHardHoldCondition); // Now, iterate of our children and tell them to terminate Iterator iter = fThreadMap.values().iterator(); while (iter.hasNext()) { STAXThread childThread = (STAXThread)iter.next(); childThread.terminate( STAXThread.THREAD_END_STOPPED_BY_PARENT); } // Now, return so we can go to sleep return; } } fState = COMPLETE; thread.popAction(); } public STAXAction cloneAction() { STAXParallelAction clone = new STAXParallelAction(); clone.fActionList = fActionList; return clone; } // STAXThreadCompleteListner method public void threadComplete(STAXThread thread, int endCode) { synchronized (fThreadMap) { fThreadMap.remove(new Integer(thread.getThreadNumber())); if (fThreadMap.isEmpty()) { fState = THREADS_COMPLETE; // thread.getParentThread() should be the thread we are // running on. thread.getParentThread().removeCondition(fHoldCondition); thread.getParentThread().removeCondition(fHardHoldCondition); thread.getParentThread().schedule(); } } } private STAXHoldThreadCondition fHoldCondition = new STAXHoldThreadCondition("Parallel"); private STAXHardHoldThreadCondition fHardHoldCondition = new STAXHardHoldThreadCondition("Parallel"); private int fState = INIT; private HashMap fThreadMap = new HashMap(); private ArrayList fActionList = new ArrayList();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -