multithreading - Simple Traffic Light Simulation with Java Threads -
as questions states, trying make very, simple traffic light simulation of 4 way intersection. problem is, using 2 different threads , trying bounce , forth between 2 using wait , notify while inside loop. have 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 call notify, not reactivate the first thread. doing wrong or there better way it?
your program have multiple issues - no conditional variable, no while loop check condition etc. below 1 possible solution. have 4 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()) { (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(); } (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(); } }
Comments
Post a Comment