Java 我的摆动计时器独立运行
我是一个普通的程序员,试图为红绿灯编程。我所有的代码都准备好了,但我的摆动计时器出了问题。我希望所有的灯泡都倒计时到零。按照这些顺序,先是红色,然后是琥珀色,最后是绿色。我想让它这样做十次,但当我在循环中运行它时,我会让计时器独立运行。我现在想要的是倒计时应该在下一次开始之前完成。循环显示在gui启动之前完成。如果可能,请帮助我进行替代或更正。期待中的感谢Java 我的摆动计时器独立运行,java,swing,timer,Java,Swing,Timer,我是一个普通的程序员,试图为红绿灯编程。我所有的代码都准备好了,但我的摆动计时器出了问题。我希望所有的灯泡都倒计时到零。按照这些顺序,先是红色,然后是琥珀色,最后是绿色。我想让它这样做十次,但当我在循环中运行它时,我会让计时器独立运行。我现在想要的是倒计时应该在下一次开始之前完成。循环显示在gui启动之前完成。如果可能,请帮助我进行替代或更正。期待中的感谢 // setting values that each bulb uses to count down
// setting values that each bulb uses to count down
private int red_time = 10;
private int amb_time = 3;
private int grn_time = 10;
private int speed = 500; // 0.5 second
Queue myqueue = new LinkedList();
Queue dupqueue = new LinkedList(); //this queue contains same elements as myqueue. used to get next signal
public int cars1;
public int cars2;
public int cars3;
public int cars4;
Timer timer;
int counter = 0;
/**
* Creates new form bulbs
*/
public bulbs() {
initComponents();
red1.setText(null);
amb1.setText(null);
grn1.setText(null);
red2.setText(null);
amb2.setText(null);
grn2.setText(null);
// the first view sees only the red bulb on
red1.setEnabled(true);
amb1.setEnabled(false);
grn1.setEnabled(false);
/* my algorithm
1. Load ← [ L1,L2,L3,L4]
2. RESULT_QUEUE ← COMPARE (LOAD)
3. NEXT_SIGNAL ← ADD(RESULT_QUEUE)
4. COUNTER ← 0
5. WHILE (!EMPTY(NEXT_SIGNAL && COUNTER MOD != 10)):
a. PREDICTION_LIST ←ADD(LOAD)
b. MOVE ← POP(NEXT_SIGNAL)
c. RELEASE (MOVE, LOAD) UNTIL TIMER ==0
d. NEXT_SIGNAL ← ADD(MOVE)
e. LOAD ← GENERATE(LOAD)
f. COUNTER++
6. RESULT_QUEUE ← PREDICT(LOAD)
7. NEXT_SIGNAL ← ADD (RESULT_QUEUE)
8. GOTO 4
9. EXIT
*/
Car car = new Car();
Compare com = new Compare();
Store store = new Store();
//prediction list for all lanes
int [] pred1 = new int [10];
int [] pred2 = new int [10];
int [] pred3 = new int [10];
int [] pred4 = new int [10];
// put cars on each lane
car.put_car(1);
car.put_car(2);
car.put_car(3);
car.put_car(4);
cars1 = car.lane1cars;
cars2 = car.lane2cars;
cars3 = car.lane3cars;
cars4 = car.lane4cars;
// setting the cars on lanes
lab1.setText(String.valueOf("CARS: "+car.lane1cars));
lab2.setText(String.valueOf("CARS: "+car.lane2cars));
lab3.setText(String.valueOf("CARS: "+car.lane3cars));
lab4.setText(String.valueOf("CARS: "+car.lane4cars));
int next_signal;
while ( counter <10 && !myqueue.isEmpty()){ // the problem area i have
//collecting useful loads for prediction
pred1[counter] = car.lane1cars;
pred2[counter] = car.lane2cars;
pred3[counter] = car.lane3cars;
pred4[counter] = car.lane4cars;
//collect next signal
next_signal = ((Integer)myqueue.remove());
System.out.println(next_signal);//this console prints completely before GUI starts running
start(next_signal);
myqueue.add(next_signal);
counter++;
}
}
请创建一个我认为您对并发性(通常)缺乏了解的示例,也许可以看看,您的
灯泡中的while循环
可能是问题的根源扫描您能帮我解决吗?
private void start(final int lane){
ActionListener action = new ActionListener(){
public void actionPerformed(ActionEvent evt){
if (red_time >= 0){
red1.setText(String.valueOf(red_time));
red_time --;
}
else if (amb_time >= 0){
amb1.setEnabled(true);
red1.setEnabled(false);
red1.setText(null);
amb1.setText(String.valueOf(amb_time));
amb_time -- ;
}
else if (grn_time >= 0){
System.out.println();
grn1.setEnabled(true);
amb1.setEnabled(false);
amb1.setText(null);
grn1.setText(String.valueOf(grn_time));
grn_time -- ;
}
else{
timer.stop();
grn1.setText(null);
grn1.setEnabled(false);
red1.setEnabled(true);
reset_time();
System.out.println (red_time);
}
}
};
timer = new Timer(speed,action);
timer.setInitialDelay(0);
timer.start();
}