Skip to content

Instantly share code, notes, and snippets.

@abhiramsingh
abhiramsingh / StopLight.java
Created March 21, 2013 02:56
The StopLight Class
package test.concurrency.traffic;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
/**
* This class is thread safe (all methods synchronized) and represents a
* StopLight The problem assumes only one StopLight on the pavement hence the
* class is made singleton
*/
@abhiramsingh
abhiramsingh / Road.java
Created March 21, 2013 02:55
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;
@abhiramsingh
abhiramsingh / Car.java
Created March 21, 2013 02:54
The Car Class
package test.concurrency.traffic;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;
public class Car implements Callable<Car> {
private static StopLight sLight = StopLight.getInstance(); // StopLight will
// be injected
// into Car
@abhiramsingh
abhiramsingh / Consts.java
Created March 21, 2013 02:52
Application Constants
package test.concurrency.traffic;
public class Consts {
public static final int TOTAL_RUN_SECONDS = 100;
public static final int PAVEMENT_LENGTH =100;
public static final int CAR_1_START_POS = 24;
public static final int CAR_2_START_POS = 12;
public static final int CAR_3_START_POS = 0;
public static final int ONE_SEC =1000;
public static final int SWITCH_INTERVAL = 10;
@abhiramsingh
abhiramsingh / LightState.java
Last active December 15, 2015 05:39
LightState
package test.concurrency.traffic;
public enum LightState {
RED, GREEN;
}
@abhiramsingh
abhiramsingh / HeapTestHarness.java
Last active December 14, 2015 10:49
Test Harness for Heap.java
package com.abhi.ds.heap;
public class HeapTestHarness {
public static void main(String a[]) {
Heap<Integer> hp = new Heap<Integer>();
hp.addItem(5);
hp.addItem(3);
hp.addItem(2);
@abhiramsingh
abhiramsingh / Heap.java
Last active December 14, 2015 10:49
Heap Data structure and Heap Sort implementation
package com.abhi.ds.heap;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
public class Heap<T extends Comparable<T>> {
private List<T> items;