我有一个基于胶囊图的 java 程序,其中我有 3 个类胶囊、端口和 state_machine。一个州的统计机 prnts 其州的 nr。程序的工作原理是这样的
面:
public class SimpleCapsule {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Port p2=new Port(null);
Capsule1 c1= new Capsule1(3,1, p2, null);
Capsule1 c2= new Capsule1(2,-3, null, p2);
//while ( false ){
c1.state.start();
c2.state.start();
// }
}
}
港口:
公共类端口{
private Port connect; public boolean signal=true; public Port (Port c){ this.connect=c; } public void snedSignal(boolean b){ if (this.connect!=null){ this.connect.signal=b; } }
public boolean getSignal(){ return this.signal; } }
胶囊:
public class Capsule1 {
public StateMachine1 state; public Port port;
public Capsule1 (int i, int signal, Port connecte, Port thisPort){
if (thisPort!=null){
this.port=thisPort;
}else{
port=new Port(connecte);
}
state= new StateMachine1 (i,signal,this.port);
}
}
状态机:
public class StateMachine1 extends Thread{
public boolean signal=false;
public int nr, signalSend;
public Port port;
public StateMachine1(int i, int signalNr, Port p){
this.nr=i;
this.signalSend=signalNr;
this.port=p;
}
@Override
public void run(){
System.out.println(nr+"started");
while (true){
if (this.port.getSignal()){
for (int i=0; i<this.nr;i++){
System.out.println(nr+"th: "+i+" sS");
if (signalSend==i){
port.snedSignal(true);
}else{
port.snedSignal(false);
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(StateMachine1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}else{
System.out.print(nr+"TH-"+this.port.getSignal()+" ");
}
}
}
}
像这样,程序运行良好,但如果你评论状态机类的最后一个原始数据: System.out.print(nr+"TH-"+this.port.getSignal()+" ");
第二个状态机SM2在第 3 个或第 4 个信号启动后没有收到。
问题是什么?我在这个信号发送过程中哪里出错了?