解决方案 »

  1.   


    synchronized (this) {
                        System.out.println("---------------------");
                        this.wait();
                        System.out.println("++++++++++++++++++++++");
                    }你这个线程wait的时候,已经锁住了this对象了,public void jixu(int num) {
            this.num = num;
            synchronized (this) {
                this.notify();
            }

        }所以这里的代码根本就进不去,notify肯定起不到效果了。
      

  2.   


    public class OutputThreadTest implements Runnable {
        public void run() {
            test2 test2 = new test2();
            test2.start();
            try {
    Thread.sleep(1000);
    //在这睡1s
    //如果不睡,test2.jixu(22)可能在test2这个线程启动前,就已经执行了
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
            test2.jixu(22);
        }    public static void main(String[] args) {
            Thread thread1 = new Thread(new OutputThreadTest());
            thread1.start();
        }
    }class test2 extends Thread {
        private int num = 0;    public void jixu(int num) {
            this.num = num;
            synchronized (this) {
                this.notify();
            }
        }    public void run() {
            try {
                while (true) {
                    synchronized (this) {
                        System.out.println("---------------------");
                        this.wait();
                        System.out.println("++++++++++++++++++++++");
                    }
                    System.out.println(num);
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    }
    }
      

  3.   

    wait notify 配合使用和synchronized一样的效果
    你都使用,有点逗
      

  4.   

    我的建议,用了synchronized就去掉wait和notify,要么去掉synchronized,你试试