📄 nodeprocessthread.java
字号:
/*
* Created on 2005-9-2
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.apollo.thread.example;
public class NodeProcessThread extends Thread {
private Node node = null;
private boolean processFlag = false;
private static boolean runningFlag = true;
public NodeProcessThread(){
this.start();
}
/**
* 终止所有NodeProcessThread线程
*/
public static void stopAll(){
runningFlag = false;
}
public void setNode(Node node){
this.node = node;
}
/**
* 开始处理节点
*/
public void startProcess(){
processFlag = true;
}
public void run(){
System.out.println("线程<" + this.getName() + ">开始处理!");
while (runningFlag) {
while (processFlag) {
//这里本来应该把节点的状态改变为”处理中“,最后还是把改变状态的动作提
//前到NodeList.getNode()中了,不然会出现节点重复处理的问题。
//
// if (node != null) {
// //在这里改状态之前,其它线程可能已经取到该节点了!
// node.setState(Node.RECEIVE_MIDMAY);
// }
this.processNextNote();
if (node != null) {
try {
node.receiveData();
} catch (InterruptedException e) {
e.printStackTrace();
}
//这里设定状态没什么问题!
node.setState(Node.RECEIVE_COMPLETE);
}
this.endOfNodeProcess();
}
}
System.out.println("线程<" + this.getName() + ">处理结束!");
}
/**
* 处理下一个节点
*/
private void processNextNote(){
NodeList list = Utility.getNodeList();
synchronized (list) {
if (list.isComplete()) {
synchronized (list) {
list.notify();
}
} else {
ThreadPool pool = Utility.getThreadPool();
synchronized (pool) {
if (pool.isEmpty()) {
return;
} else {
NodeProcessThread t = pool.pop();
t.setNode(list.getNode());
t.startProcess();
}
}
}
}
}
/**
* 节点处理的收尾工作
*/
private void endOfNodeProcess(){
this.processFlag = false; //本节点处理结束
this.node = null;
Utility.getThreadPool().push(this);
if (Utility.getNodeList().isComplete()) {
synchronized (Utility.getNodeList()) {
Utility.getNodeList().notify();
}
} else {
this.processFlag = true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -