Reputation: 73
As the questions states, I am trying to make a very, very simple traffic light simulation of a four way intersection. What my problem is, is that I am using two different threads and trying to bounce back and forth between the two using wait and notify while inside a for loop. I have some psuedo code below.
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();
}
}
}
}
But when I call notify, it does not reactivate the the first thread. Am I doing something wrong or is there a better way to do it?
Upvotes: 2
Views: 4935
Reputation: 3514
Your program have multiple issues - no conditional variable, no while loop to check condition etc. Below is one possible solution. I have four classes:
MyLock - (Lock Object)
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;
}
}
TrafficLightWorkerOne (Traffic Worker)
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();
}
}
}
}
}
TrafficLightWorkerTwo (Another Traffic Worker)
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();
}
}
}
}
TrafficLightSimulator (Main Class)
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();
}
}
Upvotes: 1