基于Java线程的简单交通灯仿真
如问题所述,我正在尝试对一个四向交叉口进行一个非常简单的交通灯模拟。我的问题是,我正在使用两个不同的线程,并试图在for循环中使用wait和notify在这两个线程之间来回跳转。下面我有一些psuedo代码基于Java线程的简单交通灯仿真,java,multithreading,Java,Multithreading,如问题所述,我正在尝试对一个四向交叉口进行一个非常简单的交通灯模拟。我的问题是,我正在使用两个不同的线程,并试图在for循环中使用wait和notify在这两个线程之间来回跳转。下面我有一些psuedo代码 public class Processor { public static int CarCounter = 1; public void lightOneAndTwo() throws InterruptedException { synchr
public class Processor
{
public static int CarCounter = 1;
public void lightOneAndTwo() throws InterruptedException
{
synchronized(this)
{
for(int carsOne = 0; carsOne < 50; carsOne++)
{
//Light One---------------------------->
//Light Two---------------------------->
wait();
}
}
}
public void lightThreeAndFour() throws InterruptedException
{
Thread.sleep(1000);
synchronized(this)
{
for(int carsTwo = 0; carsTwo < 50; carsTwo++ )
{
//Light Three---------------------------->
notify();
}
}
}
}
公共类处理器
{
公共静态整数计数器=1;
public void lightOneAndTwo()抛出InterruptedException
{
已同步(此)
{
对于(int carsOne=0;carsOne<50;carsOne++)
{
//轻一号----------------->
//第二灯----------------->
等待();
}
}
}
public void lightThreeAndFour()抛出InterruptedException
{
睡眠(1000);
已同步(此)
{
对于(int carsTwo=0;carsTwo<50;carsTwo++)
{
//三号灯----------------->
通知();
}
}
}
}
但是当我调用notify时,它不会重新激活第一个线程。我做错了什么,或者有更好的方法吗?您的程序有多个问题-没有条件变量,没有检查条件的while循环等。下面是一个可能的解决方案。我有四门课: MyLock-(锁定对象) 交通灯工作人员(交通工人)
发布一个完整的示例。notifyAll()更好问题似乎是,您的notify()调用被浪费了,因为您只是在没有任何条件的情况下调用它,并且它可能在对同一对象调用wait()之前运行。您没有使用公共锁对象
package com.test;
public class MyLock {
private volatile boolean condition;
public MyLock(boolean condition) {
this.condition = condition;
}
public boolean condition() {
return condition;
}
public void flipCondition() {
condition = !condition;
}
}
package com.test;
public class TrafficLightWorkerOne implements Runnable {
private int cars;
private MyLock lock;
public TrafficLightWorkerOne(MyLock lock, int cars) {
this.lock = lock;
this.cars = cars;
}
@Override
public void run() {
while (true) {
synchronized (lock) {
while (!lock.condition()) {
for (int carsOne = 0; carsOne < cars; carsOne++) {
System.out.println(Thread.currentThread().getName()
+ " car no : " + carsOne);
// Light One---------------------------->
// Light Two---------------------------->
}
lock.notifyAll();
lock.flipCondition();
}
try {
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package com.test;
public class TrafficLightWorkerTwo implements Runnable {
private int cars;
private MyLock lock;
public TrafficLightWorkerTwo(MyLock lock, int cars) {
this.lock = lock;
this.cars = cars;
}
@Override
public void run() {
while (true) {
synchronized (lock) {
try {
while (!lock.condition()) {
lock.wait();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int carsOne = 0; carsOne < cars; carsOne++) {
System.out.println(Thread.currentThread().getName()
+ " car no : " + carsOne);
// Light One---------------------------->
// Light Two---------------------------->
}
lock.flipCondition();;
lock.notifyAll();
}
}
}
}
package com.test;
public class TrafficLightSimulator {
public static void main(String[] args) {
boolean condition = false;
MyLock lock = new MyLock(condition);
Thread threadOne = new Thread(new TrafficLightWorkerOne(lock, 5), "One");
Thread threadTwo = new Thread(new TrafficLightWorkerTwo(lock, 4), "Two");
threadOne.start();
threadTwo.start();
}
}