当前位置: 代码迷 >> J2SE >> 线程有关问题,哪位高手看向错在哪,程序无法继续执行了
  详细解决方案

线程有关问题,哪位高手看向错在哪,程序无法继续执行了

热度:17   发布时间:2016-04-23 20:29:50.0
线程问题,谁看向错在哪,程序无法继续执行了
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 TestThreadCarForVectorForSynchrForDiffClass {

/**
 * 缺口
 */
// 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 CarThread2(car2, gaps);
Thread t3 = new CarThread3(car3, gaps);

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

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

public static void main(String[] args) {
TestThreadCarForVectorForSynchrForDiffClass t = new TestThreadCarForVectorForSynchrForDiffClass();
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;
  相关解决方案