 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
printdude1968@gmail.com Guest
|
Posted: Mon May 21, 2007 5:13 am Post subject: How to do something every 2 minutes and know which 2 minute |
|
|
Consider the following code:
package sim;
import common.*;
public class TrafficLightTester {
public static void main(String [] args) {
// create 4 lights
Light north = new Light(STATE.GO);
Light south = new Light(STATE.GO);
Light east = new Light(STATE.STOP);
Light west = new Light(STATE.STOP);
// Create 6 STATE arrays
STATE[] s1 = {STATE.GO, STATE.GO, STATE.STOP, STATE.STOP};
STATE[] s2 = {STATE.AMBER, STATE.AMBER, STATE.STOP, STATE.STOP};
STATE[] s3 = {STATE.STOP, STATE.STOP, STATE.LEFT, STATE.LEFT};
STATE[] s4 = {STATE.STOP, STATE.STOP, STATE.GO, STATE.GO};
STATE[] s5 = {STATE.STOP, STATE.STOP, STATE.AMBER, STATE.AMBER};
STATE[] s6 = {STATE.LEFT, STATE.LEFT, STATE.STOP, STATE.STOP};
int count = -1;
count++;
if (count == 0 ) {
north.setLightState(s1[0]);
south.setLightState(s1[1]);
east.setLightState(s1[2]);
west.setLightState(s1[3]);
System.out.println("North");
north.showLight();
System.out.println("South");
south.showLight();
System.out.println("East");
east.showLight();
System.out.println("West");
west.showLight();
}
if (count == 150000 ) {
north.setLightState(s2[0]);
south.setLightState(s2[1]);
east.setLightState(s2[2]);
west.setLightState(s2[3]);
System.out.println("North");
north.showLight();
System.out.println("South");
south.showLight();
System.out.println("East");
east.showLight();
System.out.println("West");
west.showLight();
}
if (count == 300000 ) {
north.setLightState(s3[0]);
south.setLightState(s3[1]);
east.setLightState(s3[2]);
west.setLightState(s3[3]);
System.out.println("North");
north.showLight();
System.out.println("South");
south.showLight();
System.out.println("East");
east.showLight();
System.out.println("West");
west.showLight();
}
if (count == 450000 ) {
north.setLightState(s4[0]);
south.setLightState(s4[1]);
east.setLightState(s4[2]);
west.setLightState(s4[3]);
System.out.println("North");
north.showLight();
System.out.println("South");
south.showLight();
System.out.println("East");
east.showLight();
System.out.println("West");
west.showLight();
}
if (count == 600000 ) {
north.setLightState(s5[0]);
south.setLightState(s5[1]);
east.setLightState(s5[2]);
west.setLightState(s5[3]);
System.out.println("North");
north.showLight();
System.out.println("South");
south.showLight();
System.out.println("East");
east.showLight();
System.out.println("West");
west.showLight();
}
if (count == 750000 ) {
north.setLightState(s6[0]);
south.setLightState(s6[1]);
east.setLightState(s6[2]);
west.setLightState(s6[3]);
System.out.println("North");
north.showLight();
System.out.println("South");
south.showLight();
System.out.println("East");
east.showLight();
System.out.println("West");
west.showLight();
// count = 0;
}
}
}
Here's what I need to do in pseudo-code
If I'm at the 0'th minute, set each light to the first state.
If I'm at the 2nd minute, set each light to the second state.
If I'm at the 3rd minute.....
etc.
I don't know enough about threads to use them so I was hoping I could
simulate this by using an incremented integer value, but I would
suspect that from reading other threads in this group, there's no
really accurate way since the JVM will increment them as fast as it
can.
My code works in that it is setting things correctly, but I only want
them to be set at 2 minute intervals, otherwise, there's no way that
traffic will be able to flow. |
|
| Back to top |
|
 |
printdude1968@gmail.com Guest
|
Posted: Mon May 21, 2007 6:04 am Post subject: Re: How to do something every 2 minutes and know which 2 min |
|
|
package sim;
import common.*;
public class TrafficLightTester {
public static void main(String [] args) {
final int twoMinutes=120000;
// create 4 lights
Light north = new Light(STATE.GO);
Light south = new Light(STATE.GO);
Light east = new Light(STATE.STOP);
Light west = new Light(STATE.STOP);
// Create 6 STATE arrays
STATE[] s1 = {STATE.GO, STATE.GO, STATE.STOP, STATE.STOP};
STATE[] s2 = {STATE.AMBER, STATE.AMBER, STATE.STOP, STATE.STOP};
STATE[] s3 = {STATE.STOP, STATE.STOP, STATE.LEFT, STATE.LEFT};
STATE[] s4 = {STATE.STOP, STATE.STOP, STATE.GO, STATE.GO};
STATE[] s5 = {STATE.STOP, STATE.STOP, STATE.AMBER, STATE.AMBER};
STATE[] s6 = {STATE.LEFT, STATE.LEFT, STATE.STOP, STATE.STOP};
int count = 0;
while (count <= 6 )
{
count++;
if (count == 1 ) {
north.setLightState(s1[0]);
south.setLightState(s1[1]);
east.setLightState(s1[2]);
west.setLightState(s1[3]);
System.out.println("North");
north.showLight();
System.out.println("South");
south.showLight();
System.out.println("East");
east.showLight();
System.out.println("West");
west.showLight();
}
if (count == 2 ) {
north.setLightState(s2[0]);
south.setLightState(s2[1]);
east.setLightState(s2[2]);
west.setLightState(s2[3]);
System.out.println("North");
north.showLight();
System.out.println("South");
south.showLight();
System.out.println("East");
east.showLight();
System.out.println("West");
west.showLight();
}
if (count == 3 ) {
north.setLightState(s3[0]);
south.setLightState(s3[1]);
east.setLightState(s3[2]);
west.setLightState(s3[3]);
System.out.println("North");
north.showLight();
System.out.println("South");
south.showLight();
System.out.println("East");
east.showLight();
System.out.println("West");
west.showLight();
}
if (count == 4 ) {
north.setLightState(s4[0]);
south.setLightState(s4[1]);
east.setLightState(s4[2]);
west.setLightState(s4[3]);
System.out.println("North");
north.showLight();
System.out.println("South");
south.showLight();
System.out.println("East");
east.showLight();
System.out.println("West");
west.showLight();
}
if (count == 5 ) {
north.setLightState(s5[0]);
south.setLightState(s5[1]);
east.setLightState(s5[2]);
west.setLightState(s5[3]);
System.out.println("North");
north.showLight();
System.out.println("South");
south.showLight();
System.out.println("East");
east.showLight();
System.out.println("West");
west.showLight();
}
if (count == 6 ) {
north.setLightState(s6[0]);
south.setLightState(s6[1]);
east.setLightState(s6[2]);
west.setLightState(s6[3]);
System.out.println("North");
north.showLight();
System.out.println("South");
south.showLight();
System.out.println("East");
east.showLight();
System.out.println("West");
west.showLight();
count = 0;
}
Thread t = new Thread();
t.start();
try {
Thread.sleep(twoMinutes);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|