📄 staxthread.java
字号:
public void setSignalMsgVar(String signalMsgVarName, String elementInfo) { setSignalMsgVar(signalMsgVarName, elementInfo, null); } public void setSignalMsgVar(String signalMsgVarName, String elementInfo, STAXPythonEvaluationException pythonException) { // Generate a message containing information about why the signal // was raised using information provided. StringBuffer msg = new StringBuffer( "\n\n===== Element Information =====\n\n"); msg.append(elementInfo); if (pythonException != null) { msg.append("\n\n===== Python Error Information =====\n\n"); msg.append(pythonException); msg.append("\n"); } else { msg.append("\n\n"); } msg.append("===== Call Stack for STAX Thread "); msg.append(fThreadNumber); msg.append(" =====\n\n"); msg.append(STAFMarshallingContext.formatObject(getCallStack())); List conditionStack = this.getConditionStack(); if (conditionStack.size() > 0) { msg.append("\n\n==== Condition Stack for STAX Thread "); msg.append(fThreadNumber); msg.append(" =====\n\n"); msg.append(STAFMarshallingContext.formatObject( getConditionStack())); } // Set the message value to the signal message variable name pySetVar(signalMsgVarName, msg.toString()); } public void handledSignal(String name) { if ((fSignalStack.size() == 0) || (!((String)fSignalStack.getLast()).equals(name))) { System.out.println("We aren't currently handling signal: " + name); // XXX: Not good } else fSignalStack.removeLast(); } public List getCallStack() { // Generate the call stack List callStack = new ArrayList(); this.visitActions(new STAXVisitorHelper(callStack) { public void visit(Object o, Iterator iter) { STAXAction action = (STAXAction)o; String className = action.getClass().getName(); if (className.startsWith(STAX.PACKAGE_NAME)) className = className.substring(STAX.PACKAGE_NAME.length()); String classSuffix = "Action"; if (className.endsWith(classSuffix)) { className = className.substring( 0, className.length() - classSuffix.length()); } List callStack = (ArrayList)fData; callStack.add(className + ": " + action.getInfo()); } }); return callStack; } public List getConditionStack() { // Generate the condition stack List conditionStack = new ArrayList(); this.visitConditions(new STAXVisitorHelper(conditionStack) { public void visit(Object o, Iterator iter) { STAXCondition condition = (STAXCondition)o; String className = condition.getClass().getName(); if (className.startsWith(STAX.PACKAGE_NAME)) className = className.substring(STAX.PACKAGE_NAME.length()); String classSuffix = "Condition"; if (className.endsWith(classSuffix)) { className = className.substring( 0, className.length() - classSuffix.length()); } List conditionStack = (ArrayList)fData; conditionStack.add( className + ": " + "Source=" + condition.getSource() + ", Priority=" + condition.getPriority()); } }); return conditionStack; } // // State functions // public int getState() { return fState; } public String getStateAsString() { switch (fState) { case STATE_RUNNABLE: return STATE_RUNNABLE_STRING; case STATE_RUNNING: return STATE_RUNNING_STRING; case STATE_BLOCKED: return STATE_BLOCKED_STRING; case STATE_COMPLETE: return STATE_COMPLETE_STRING; default: return STATE_UNKNOWN_STRING; } } // XXX: Might we need an inheritCondition in addition to addCondition? // Would this make the logic easier here and in the conditions? public boolean addCondition(STAXCondition condition) { synchronized (fConditionSet) { return fConditionSet.add(condition); } } public boolean removeCondition(STAXCondition condition) { synchronized (fConditionSet) { return fConditionSet.remove(condition); } } public void visitConditions(STAXVisitor visitor) { synchronized (fConditionSet) { Iterator iter = fConditionSet.iterator(); while (iter.hasNext()) visitor.visit(iter.next(), iter); } } // // Execution functions // public void pushAction(STAXAction action) { synchronized (fActionStack) { fActionStack.addLast(action); } } public void popAction() { synchronized (fActionStack) { fActionStack.removeLast(); } } public void visitActions(STAXVisitor visitor) { synchronized (fActionStack) { Iterator iter = fActionStack.iterator(); while (iter.hasNext()) visitor.visit(iter.next(), iter); } } public void schedule() { synchronized (fConditionSet) { if (fState == STATE_BLOCKED) { fState = STATE_RUNNABLE; fJob.getSTAX().getThreadQueue().add(this); } } } public void execute() { int numActions = 0; fState = STATE_RUNNING; while (true) { if (++numActions == MAX_NON_BLOCKING_ACTIONS) { synchronized (fConditionSet) { fState = STATE_BLOCKED; schedule(); return; } } if (fActionStack.size() == 0) { synchronized (fChildMap) { synchronized (fConditionSet) { if (fChildMap.size() != 0) { fState = STATE_BLOCKED; return; } } } fState = STATE_COMPLETE; synchronized (fConditionSet) { boolean addedParentCondition = false; Iterator iter = fConditionSet.iterator(); while (iter.hasNext()) { STAXCondition cond = (STAXCondition)iter.next(); // XXX: We need to get the parent running again so // that it may kill any children threads if // necessary // XXX: Is it safe to schedule the parent? // XXX: Should addCondition call schedule()? if ((fParent == null) && cond.isInheritable()) { fJob.log(STAXJob.JOB_LOG, "error", "Unhandled " + "condition of type " + cond.getClass().getName() + " found at " + "end of job"); } else if (cond.isInheritable()) { fParent.addCondition(cond); addedParentCondition = true; } } if (addedParentCondition) fParent.schedule(); } if (STAX_DEBUG_THREAD) { System.out.println("Thread #" + fThreadNumber + ": complete"); } while (!fCompletionNotifiees.isEmpty()) { STAXThreadCompleteListener listener = (STAXThreadCompleteListener) fCompletionNotifiees.removeFirst(); if (listener != null) listener.threadComplete(this, fCompletionCode); } return; } STAXAction action = (STAXAction)fActionStack.getLast(); STAXCondition currCondition = null; synchronized (fConditionSet) { if (fConditionSet.size() != 0) { currCondition = (STAXCondition)fConditionSet.first(); } if ((currCondition != null) && ((currCondition instanceof STAXHoldThreadCondition) || (currCondition instanceof STAXHardHoldThreadCondition))) { if (STAX_DEBUG_THREAD) { String condName = currCondition.getClass().getName(); condName = condName.substring( condName.lastIndexOf(".") + 1); System.out.println("Thread #" + fThreadNumber + ": blocking due to " + condName); } fState = STATE_BLOCKED; return; } } if (currCondition == null) { if (STAX_DEBUG_THREAD) { String actionName = action.getClass().getName(); actionName = actionName.substring( actionName.lastIndexOf(".") + 1); System.out.println("Thread #" + fThreadNumber + ": calling " + actionName); } action.execute(this); } else { if (STAX_DEBUG_THREAD) { String actionName = action.getClass().getName(); actionName = actionName.substring( actionName.lastIndexOf(".") + 1); String condName = currCondition.getClass().getName(); condName = condName.substring( condName.lastIndexOf(".") + 1); System.out.println("Thread #" + fThreadNumber + ": calling " + actionName + ", handling " + condName); } action.handleCondition(this, currCondition); } } // end while } public void terminate(int termCode) { synchronized (fConditionSet) { fCompletionCode = termCode; addCondition(new STAXTerminateThreadCondition("Thread")); schedule(); } } // STAXThreadCompleteListener method public void threadComplete(STAXThread thread, int endCode) { synchronized(fChildMap) { fChildMap.remove(thread.getThreadNumberAsInteger()); if (fChildMap.size() == 0) schedule(); } } // This class is used to sort the conditions in the condition set class STAXConditionComparator implements Comparator { public int compare(Object o1, Object o2) { STAXCondition c1 = (STAXCondition)o1; STAXCondition c2 = (STAXCondition)o2; if (c1.getPriority() == c2.getPriority()) { if (o1.hashCode() == o2.hashCode()) return 0; else if (o1.hashCode() < o2.hashCode()) return -1; } else if (c1.getPriority() < c2.getPriority()) return -1; return 1; } } private int fState = STATE_BLOCKED; private int fCompletionCode = THREAD_END_OK; private String fTerminationBlock = new String(); private STAXJob fJob = null; private int fThreadNumber = 0; private STAXThread fParent = null; private HashMap fSignalHandlerMap = new HashMap(); private STAXPythonInterpreter fPythonInterpreter = new STAXPythonInterpreter(); private LinkedList fActionStack = new LinkedList(); private LinkedList fSignalStack = new LinkedList(); private TreeSet fHeldBlocksSet = new TreeSet(); private TreeSet fConditionSet = new TreeSet(new STAXConditionComparator()); private LinkedList fCompletionNotifiees = new LinkedList(); private TreeMap fChildMap = new TreeMap(); private STAXTimestamp fStartTimestamp;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -