Thread題不知道錯在了那裡

瓜瓜東西發表於2014-07-28
package com.wanju.project001.zonghe.test;

import java.util.Vector;
import java.util.Vector;
import java.util.Map;
import java.util.Random;

import com.sun.org.apache.bcel.internal.generic.NEW;

public class TestThreadCarForVectorForSynchr {

	/**
	 * 缺口
	 */
	// public static final int gap = new Random().nextInt(10);
	public static final int gap = 1;
	public static Vector<Integer> gaps = new Vector<Integer>();

	public void init() {
		for (int i = 0; i < gap; i++) {
			gaps.add(i);
		}
	}

	public void test() {
		Car car1 = new Car(1);
		Car car2 = new Car(2);
		Car car3 = new Car(3);

		Thread t1 = new CarThread(car1, gaps);
		Thread t2 = new CarThread(car2, gaps);
		Thread t3 = new CarThread(car3, gaps);

		CarThread.Vector.add(t1);
		CarThread.Vector.add(t2);
		CarThread.Vector.add(t3);

		long start = System.currentTimeMillis();
		while (System.currentTimeMillis() - start < 1000) {
		}
		t1.start();
		t2.start();
		t3.start();
	}

	public static void main(String[] args) {
		TestThreadCarForVectorForSynchr t = new TestThreadCarForVectorForSynchr();
		t.init();
		t.test();
	}
}

class CarThread extends Thread {
	private Car car;

	public Car getCar() {
		return car;
	}

	public void setCar(Car car) {
		this.car = car;
	}

	private static boolean flg = false;
	private static boolean flgRoad = true;
	public static Vector<Thread> Vector = new Vector<Thread>();

	public static boolean isFlg() {
		return flg;
	}

	public static void setFlg(boolean flg) {
		CarThread.flg = flg;
	}

	public CarThread() {
		// TODO Auto-generated constructor stub
	}

	public static void initThreadGroup(Thread t) {
		Vector.add(t);
	}

	public static void initCar(Vector<Integer> gaps) {
		if (!flg) {
			Car.gaps.addAll(gaps);
			flg = true;
		}
	}

	public CarThread(Car car, Vector<Integer> gaps) {
		this.car = car;
		this.setName("CarThread---" + car.getNum());
		initCar(gaps);
	}

	public synchronized RoadStatus getRoadStatus() {
		RoadStatus rStatus = car.getRoadStatusByRandom();
		return rStatus;
	}

	@Override
	public void run() {
		while (flgRoad) {

			RoadStatus rStatus = getRoadStatus();
			if (rStatus == RoadStatus.OK
					&& !Thread.currentThread().isInterrupted()) {
				System.out.println("Car" + car.getNum() + "執行正常");
			} else {
				repair();

			}
			if (Car.gaps.size() == 0) {
				flgRoad = false;
			}
			long start = System.currentTimeMillis();
			while (System.currentTimeMillis() - start < 1000) {
			}
		}
		// System.out.println("路已經修好,可以暢通無阻了");
	}

	public synchronized void repair() {
		Map<Thread, StackTraceElement[]> maps = Thread.getAllStackTraces();
		for (int i = 0; i < Vector.size(); i++) {
			Thread thread = Vector.get(i);
			synchronized (thread) {
				if (Vector.get(i).getName().equals(this.getName())) {
					System.out.println("Car" + car.getNum() + "發現了缺口,開始修理缺口"
							+ car.getCurrentGap() + "...");
					// this.getCar().setStatus(STATUS.REPAIRED);
				} else {

					CarThread t = null;
					if (thread instanceof CarThread) {
						t = (CarThread) thread;
						t.getCar().setStatus(STATUS.STOP);
					}
					// t.interrupt();
					try {
						thread.wait();
						System.out.println("can i deal?");
					} catch (InterruptedException e) {
					}
				}
			}
		}
		long start = System.currentTimeMillis();
		while (System.currentTimeMillis() - start < 100) {
		}

		this.getCar().setStatus(STATUS.REPAIRED);
		for (int i = 0; i < Vector.size(); i++) {
			Thread thread = Vector.get(i);
			synchronized (thread) {
				if (thread.getName().equals(this.getName())) {
					// 已經是執行狀態不需要喚醒
					continue;
				}

				CarThread t = null;
				if (thread instanceof CarThread) {
					t = (CarThread) thread;
					t.getCar().setStatus(STATUS.RERUN);
					// t.notify();
				}
			}
		}
	}
}

class Car {

	public static Vector<Integer> gaps = new Vector<Integer>();
	private int num;
	private String name;
	private STATUS status = STATUS.RUN;
	private RoadStatus roadStatus = RoadStatus.OK;

	private int currentGap = -100;

	public int getCurrentGap() {
		return currentGap;
	}

	public void setCurrentGap(int currentGap) {
		this.currentGap = currentGap;
	}

	public int getNum() {
		return num;
	}

	public static void setGaps(Vector<Integer> gaps) {
		gaps.addAll(gaps);
	}

	public STATUS getStatus() {
		return status;
	}

	public void setStatus(STATUS status) {
		this.status = status;
		switch (status) {
		case RUN:
			System.out.println("Car" + num + "開始通行..");
			break;
		case STOP:
			System.out.println("Car" + num + "開始停下等待..");
			break;
		case RERUN:
			System.out.println("Car" + num + "恢復執行");
			break;
		case REPAIRED:
			System.out.println("Car" + num + "缺口" + currentGap + "修理完成");
			break;
		default:
			break;
		}

	}

	public RoadStatus getRoadStatus() {
		return roadStatus;
	}

	public synchronized RoadStatus getRoadStatusByRandom() {
		Random random = new Random();
		int i = random.nextInt(2);
		if (i == 0) {
			return RoadStatus.OK;
		} else {
			{
				int g = getGap();
				if (g == -1000) {
					// 已經沒有了缺口
					return RoadStatus.OK;
				}
				System.out.println("Car" + num + "碰到了缺口" + g);
				gaps.remove(new Integer(g));
				this.setCurrentGap(g);
				return RoadStatus.BROKEN;
			}
		}
		// return i==0 ? RoadStatus.OK:RoadStatus.BROKEN;
	}

	public synchronized int getGap() {
		synchronized (gaps) {
			if (gaps.size() == 0) {
				return -1000;
			}
			int g = gaps.get(new Random().nextInt(gaps.size()));
			gaps.remove(new Integer(g));
			return g;
		}
	}

	public void setRoadStatus(RoadStatus roadStatus) {
		this.roadStatus = roadStatus;
	}

	public Car() {
	}

	public Car(int num) {
		this.num = num;
	}

	public Car(int num, String name) {
		this.num = num;
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

enum STATUS {
	RUN, STOP, REPAIRED, RERUN
}

enum RoadStatus {
	OK, BROKEN
}


相關文章