我在一本书中做了一个例子,它有一个线程示例,其中模拟了交通信号灯。我理解大部分程序,但是,我对这本书中“单独的线程将运行每个红绿灯”的部分感到困惑,似乎在主应用程序中只创建了一个线程? 我有一个枚举,列出了我的红绿灯的所有常量。
public class TrafficLightSimulator implements Runnable {
private Thread thread;//holds the thread that runs the simulation
private TrafficLightColor color;//holds the traffic light color
boolean stop=false;//set to true to stop the simulation
boolean changed=false;//true when the light has changed
public TrafficLightSimulator(TrafficLightColor inital){
color=inital;
thread=new Thread(this);
thread.start();
}
public TrafficLightSimulator(){
color=TrafficLightColor.RED;
thread=new Thread(this);
thread.start();
}
@Override
public void run() {
//start up the light
while(!stop){
try{
switch(color){
case GREEN:
Thread.sleep(10000);//green sleeps for 10 seconds
break;
case RED:
Thread.sleep(2000);//yellow for 2 seconds
break;
case YELLOW:
Thread.sleep(12000);//red for 12 seconds
break;
}
}catch(Exception e){
}
changeColor();
}
}
synchronized void changeColor(){
switch(color){
case RED:
color=TrafficLightColor.GREEN;
break;
case YELLOW:
color=TrafficLightColor.RED;
break;
case GREEN:
color=TrafficLightColor.YELLOW;
}
changed=true;
System.out.println("Notfiy Called We changed the light");
notify();
}
synchronized void waitForChange(){
try{
while(!changed){
System.out.println("waiting for Light to change");
wait();
}
changed=false;
}catch(Exception e){
}
}
synchronized TrafficLightColor getColor(){
return color;
}
synchronized void cancel(){
stop=true;
}
}
class Demo{
public static void main(String[]args){
TrafficLightSimulator t1=new TrafficLightSimulator(TrafficLightColor.YELLOW);
for(int i=0;i<9;i++){
System.out.println(t1.getColor());
t1.waitForChange();
}
t1.cancel();
}
}
这是该计划的其余部分。
arr.splice(0,arr.length)
答案 0 :(得分:0)
在每个println
命令中打印一个线程的名称,它会显示哪个线程正在执行代码的哪一部分,它可以帮助您了解正在进行的操作。
只是:
System.out.println(Thread.currentThread().getName() + "==>"
+ "Notfiy Called We changed the light");
如果您运行该应用程序,您将获得:
Thread-0==> start up the light
main==>YELLOW
main==>waiting for Light to change
Thread-0==>Notfiy Called We changed the light
main==>RED
main==>waiting for Light to change
Thread-0==>Notfiy Called We changed the light
main==>GREEN
main==>waiting for Light to change
Thread-0==>Notfiy Called We changed the light
main==>YELLOW
main==>waiting for Light to change
Thread-0==>Notfiy Called We changed the light
main==>RED
main==>waiting for Light to change
Thread-0==>Notfiy Called We changed the light
main==>GREEN
main==>waiting for Light to change
Thread-0==>Notfiy Called We changed the light
main==>YELLOW
main==>waiting for Light to change
Thread-0==>Notfiy Called We changed the light
main==>RED
main==>waiting for Light to change
Thread-0==>Notfiy Called We changed the light
main==>GREEN
main==>waiting for Light to change
Thread-0==>Notfiy Called We changed the light
Thread-0==>Notfiy Called We changed the light
每个java应用程序中始终有一个 main 线程 - 该线程由Java虚拟机创建,主线程执行应用程序代码(调用public static void main()
方法)。登记/>
应用程序可以创建和启动其他线程 - 在您的代码中,它的名称是Thread-0
,它在构造函数中创建并启动 - 这里:
public TrafficLightSimulator(TrafficLightColor inital) {
color = inital;
thread = new Thread(this); // creates a new thread
thread.start(); // starts the new thread
}
请复制,粘贴并运行此代码,每个Thread.currentThread().getName() + "==>"
命令中都包含println
:
public class TrafficLightSimulator implements Runnable {
private Thread thread;// holds the thread that runs the simulation
private TrafficLightColor color;// holds the traffic light color
boolean stop = false;// set to true to stop the simulation
boolean changed = false;// true when the light has changed
public TrafficLightSimulator(TrafficLightColor inital) {
color = inital;
thread = new Thread(this);
thread.start();
}
public TrafficLightSimulator() {
color = TrafficLightColor.RED;
thread = new Thread(this);
thread.start();
}
@Override
public void run() {
// start up the light
System.out.println(Thread.currentThread().getName() +"==>" +" start up the light" );
while (!stop) {
try {
switch (color) {
case GREEN:
Thread.sleep(10000);// green sleeps for 10 seconds
break;
case RED:
Thread.sleep(2000);// yellow for 2 seconds
break;
case YELLOW:
Thread.sleep(12000);// red for 12 seconds
break;
}
} catch (Exception e) {
}
changeColor();
}
}
synchronized void changeColor() {
switch (color) {
case RED:
color = TrafficLightColor.GREEN;
break;
case YELLOW:
color = TrafficLightColor.RED;
break;
case GREEN:
color = TrafficLightColor.YELLOW;
}
changed = true;
System.out.println(Thread.currentThread().getName() + "==>" + "Notfiy Called We changed the light");
notify();
}
synchronized void waitForChange() {
try {
while (!changed) {
System.out.println(Thread.currentThread().getName() + "==>" + "waiting for Light to change");
wait();
}
changed = false;
} catch (Exception e) {
}
}
synchronized TrafficLightColor getColor() {
return color;
}
synchronized void cancel() {
stop = true;
}
public static void main(String[] args) {
TrafficLightSimulator t1 = new TrafficLightSimulator(TrafficLightColor.YELLOW);
for (int i = 0; i < 9; i++) {
System.out.println(Thread.currentThread().getName() + "==>" + t1.getColor());
t1.waitForChange();
}
t1.cancel();
}
}