public class Car extends Thread {
        static int[] a = { 0, 0, 0 };
        static boolean isFull = false;
        private String s;        public Car(String s) {
                super();
                this.s = s;
        }
        
        public static void  carIn(){
                if (a[0] + a[1] + a[2] == 0) {
                        for (int i = 0; i < a.length; i++) {
                                a[i] = i + 1;
                                System.out.println(i + 1 + "号车进入" + i + "位置");
                        }
                } else {
                        for (int i = 0; i < a.length; i++) {
                                if(i<2){
                                a[i] = a[i+1] ;
                                System.out.println(a[i+1]  + "号车进入" + i + "位置");
                                }else{
                                        a[i]=a[i]+1;
                                        System.out.println(a[i]+1  + "号车进入" + i + "位置");
                                }
                        }
                }
                isFull = true;
        }        public static void carOut(){
                System.out.println(a[0] + "号车从0号位置离开了");
                a[0]=0;
                isFull = false;
        }
        
        public void run() {
                if (s.equals("in")) {
                        while (true) {
                                synchronized (this) {
                                        if (isFull == true) {
                                                notifyAll();
                                                try {
                                                        wait();
                                                } catch (InterruptedException e) {
                                                        e.printStackTrace();
                                                }
                                        } else {
                                                carIn();
                                        }
                                }
                        }
                } else {
                        while (true) {
                                synchronized (this) {
                                        if (isFull == false) {
                                                notifyAll();
                                                try {
                                                        wait();
                                                } catch (InterruptedException e) {
                                                        e.printStackTrace();
                                                }
                                        } else {
                                                carOut();
                                        }
                                }
                        }                }
        }        public static void main(String[] args) {
                Car c1 = new Car("in");
                Car c2 = new Car("out");
                c1.start();
                c2.start();
        }
}

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【CLK1982】截止到2008-07-09 09:57:55的历史汇总数据(不包括此帖):
    发帖的总数量:0                        发帖的总分数:0                        每贴平均分数:0                        
    回帖的总数量:0                        得分贴总数量:0                        回帖的得分率:0%                       
    结贴的总数量:0                        结贴的总分数:0                        
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:---------------------结分的百分比:---------------------
    无满意结贴率:---------------------无满意结分率:---------------------
    如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
      

  2.   

    连个单独的线程在自己对象锁上分别 wait(). notifyall()不能唤醒另一个线程的. 不同的对象阿
      

  3.   

    我大概修改了一下,你看看。
    public class Car {
    int[] a = { 0, 0, 0 };
    boolean isFull = false;
    String s; public Car() {

    } public void carIn() {
    if (a[0] + a[1] + a[2] == 0) {
    for (int i = 0; i < a.length; i++) {
    a[i] = i + 1;
    System.out.println(i + 1 + "in " + i + "location");
    }
    } else {
    for (int i = 0; i < a.length; i++) {
    if (i < 2) {
    a[i] = a[i + 1];
    System.out.println(a[i + 1] + "in " + i + "location");
    } else {
    a[i] = a[i] + 1;
    System.out.println(a[i] + 1 + "in " + i + "location");
    }
    }
    }
    isFull = true;
    } public void carOut() {
    System.out.println(a[0] + "out form 0 ");
    a[0] = 0;
    isFull = false;
    } public static void main(String[] args) throws InterruptedException {
    Car c1 = new Car();
    c1.setS("in");
    new T(c1).start();
    Thread.sleep(2000);
    c1.setS("out");
    new T(c1).start();
    } public void setS(String s) {
    this.s = s;
    }
    }
    class T extends Thread {
    Car car = null;
    T(Car car) {
    this.car = car;
    }
    public void run() {
    if (car.s.equals("in")) {
    while (true) {
    synchronized (car) {
    if (car.isFull == true) {
    try {
    car.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    } else {
    car.carIn();
    car.notifyAll();
    }
    }
    }
    } else {
    while (true) {
    synchronized (car) {
    if (car.isFull == false) {
    try {
    car.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    } else {
    car.carOut();
    car.notifyAll();
    }
    }
    } }
    }
    }