Skip to content

Instantly share code, notes, and snippets.

@abhiramsingh
Created March 21, 2013 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhiramsingh/5210361 to your computer and use it in GitHub Desktop.
Save abhiramsingh/5210361 to your computer and use it in GitHub Desktop.
The Road Class
package test.concurrency.traffic;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
/**
* This class represents a Road holding 3 car instances of type Thread and a
* Singleton StopLight instance.
*/
public class Road {
private static final int NTHREADS = 3;
private static final ExecutorService theExecutorService = Executors
.newFixedThreadPool(NTHREADS);
private static final CompletionService<Car> theCompletionService = new ExecutorCompletionService<Car>(
theExecutorService);
private static List<Future<Car>> theFutures = new ArrayList<Future<Car>>();
private static Car c1 = new Car(new AtomicInteger(Consts.CAR_1_START_POS), "Car1");
private static Car c2 = new Car(new AtomicInteger(Consts.CAR_2_START_POS), "Car2");
private static Car c3 = new Car(new AtomicInteger(Consts.CAR_3_START_POS), "Car3");
private static void startTraffic() throws InterruptedException,
ExecutionException {
addCars();
manageSingal();
waitForTheExecutor();
}
private static void waitForTheExecutor() throws InterruptedException {
while (!theExecutorService.isTerminated()) {
}
if (!theFutures.isEmpty()) {
for (int i = 0, size = theFutures.size(); i < size; ++i) {
try {
final Car result = theCompletionService.take().get();
if (result != null) {
System.out.println(result.getName()+" stopped running...");
}
} catch (ExecutionException e) {
System.out.println("Error, " + e);
}
}
}
}
private static List<Future<Car>> addCars() {
theFutures.add(theCompletionService.submit(c1));
theFutures.add(theCompletionService.submit(c2));
theFutures.add(theCompletionService.submit(c3));
theExecutorService.shutdown();
return theFutures;
}
private static void manageSingal() {
final StopLight sLight = StopLight.getInstance();
final StopLight.StopWatch sWatch = StopLight.StopWatch.getInstance();
while (sWatch.isRunning()) { // Simulating 100 seconds run with a stopwatch
System.out.println(sWatch.getElapsedTimeSecs() + " "
+ StopLight.getState() + " " + c1.getPos() + " "
+ c2.getPos() + " " + c3.getPos());
synchronized (sLight) { // StopLight instance used by the Road
// as Object Monitor to wake up cars
// waiting on the signal
sWatch.aSecond();
if (sWatch.getElapsedTimeSecs() >= Consts.SWITCH_INTERVAL
&& sWatch.getElapsedTimeSecs()
% Consts.SWITCH_INTERVAL == 0) { // when elapsed time mod switch_interval equals 0
StopLight.toggle(); // Change the signal
sLight.notifyAll(); // Notify cars waiting on StopLight
}
}
if (sWatch.getElapsedTimeSecs() > Consts.TOTAL_RUN_SECONDS) {
sWatch.stop();
}
}
}
public static void main(String args[]) {
try {
Road.startTraffic();
} catch (InterruptedException e) {
System.out.println("Error, " + e);
} catch (ExecutionException e) {
System.out.println("Error, " + e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment