使用java多线程的交通灯程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21816143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:50:30  来源:igfitidea点击:

traffic light program using java multithreading

javamultithreading

提问by user3316769

i receive this "Exception in thread "main" java.lang.NullPointerException error" in two places which i will identify by using "->" i have traced the variables and i cant seem to find where it goes wrong.

我在两个地方收到这个“线程“main”java.lang.NullPointerException 错误中的异常,我将通过使用“->”来识别我已经跟踪了变量,我似乎无法找到它出错的地方。

//simulation.java




public class Simulation  {

private Thread thread1, thread2, thread3, thread4;


 public static void main(String[] args) {
    Simulation s = new Simulation();
  -> s.go();

}
   public void Simulation(){


    TrafficLight light = new TrafficLight();
    RoadRunnable road1 = new RoadRunnable(1, light);
    RoadRunnable road2 = new RoadRunnable(2, light);
    RoadRunnable road3 = new RoadRunnable(3, light);
    RoadRunnable road4 = new RoadRunnable(4, light);
    road1.add(" ");
    road2.add(" ");
    road3.add(" ");
    road4.add(" ");
    thread1 = new Thread((Runnable) road1);
    thread2 = new Thread((Runnable) road2);
    thread3 = new Thread((Runnable) road3);
    thread4 = new Thread((Runnable) road4);
}

 public void go() 
 {
->     thread1.start();
    thread2.start();
    thread3.start();
    thread4.start();
}

}



//roadrunnable.java

import java.util.LinkedList;





public class RoadRunnable extends TrafficLight implements Runnable {

private LinkedList<String> queue;
private int number;
private TrafficLight light;

public RoadRunnable(int roadNumber, TrafficLight aLight) {
    number = roadNumber;
    light = aLight;
    queue = new LinkedList();
}



public void run() {

    while (!queue.isEmpty()) {
        light.turnGreen(number);
        queue.remove();
    }

}

 public void add(String car) {

    for (int i = 0; i < 10; i++) {
        queue.add(car);
    }
}


}



//trafficlight.java




public class TrafficLight {


private int rNumber;


public TrafficLight() {
}


public void turnGreen(int roadNumber) {
    rNumber = roadNumber;
    synchronized (this) {

        System.out.print("Light turned green by road" + rNumber
                +"\n"
                + "Waiting for road" + rNumber + "car to clear intersection \n");

        for (int i = 10; (i >= 0); i--) {

            System.out.print(i + " ");

            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
            }
        }
        System.out.print("\n \n");
    }

}
}

回答by fgb

public void Simulation() {
}

This is not a constructor. Constructors don't have a return type. Instead use:

这不是构造函数。构造函数没有返回类型。而是使用:

public Simulation() {
}