package LIU;public class T extends Thread{ private int num;
private int MAX = 50;

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

public void run()
{
synchronized(this)
{
while(this.num < 50)
{
System.out.println(this.num);
try
{
System.out.println(this.num);
this.wait();
System.out.println(this.num);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println(this.num);
this.num += 2;
this.notify();
}
}

static class Tj extends Thread
{
private int num;
private int MAX = 50;

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

public void run()
{
synchronized(this)
{
while(this.num < 50)
{
try
{
this.wait();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println(this.num);
this.num += 2;
this.notify();
}

}
}

public static void main(String[] args) {

(new Thread(new T(1))).start();
(new Thread(new Tj(0))).start();
}}
程序的大致意思就是利用线程控制交替输出奇数偶数,但是我的程序不知道哪里有问题,设置打印语句后发现wait后面的语句没有执行,我不知道是什么情况,应该怎么修改

解决方案 »

  1.   

    两个线程停在两个不同的资源上,你notify的时候只能唤醒等待在本资源的线程不能唤醒其他资源上等待的线程。所有他们怎么可能会执行、、、
      

  2.   

    有几个问题
    1. 2个线程同时wait,就同时在等待,下面的根本没法输出,需要加一个flag来控制一个等待一个继续
    2. 不需要写2个class, 一个共同的class PrintNumber就可以了。因为有共同点
    3. 线程里面需要循环打印奇数和偶数。
    4. 建议使用 ReentrantLock 配合 condition来写。/**
     * this is the sample of print using 2 threads
     * 
     */
    public final class ThreadPrintSample {
        /** the flag represent is print the odd number first **/
        private static volatile boolean isPrintOdd = true;    /**
         * static inner class using print the Odd number
         * 
         */
        static class PrintNumber extends Thread {
            private int num;
            private Object instance;        public PrintNumber(int num, Object instance) {
                this.num = num;
                this.instance = instance;
            }        public void run() {
                synchronized (instance) {
                    final Thread current = Thread.currentThread();
                    final String threadName = current.getName();                while (true) {
                        if (current.isInterrupted()) {
                            System.out.println("the current thread isInterrupted,"
                                    + " thread name: " + threadName);
                            break;
                        }                    try {
                            if (isPrintOdd) {
                                isPrintOdd = false;
                                instance.wait();
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }                    if (threadName.equalsIgnoreCase("Odd")) {
                            System.out.println("Print Odd Number, " + "number:"
                                    + this.num);
                        } else {
                            System.out.println("Print Even Number, number:"
                                    + this.num);
                        }                    this.num += 2;
                        instance.notify();
                        isPrintOdd = true;                    try {
                            Thread.sleep(1000L);
                        } catch (InterruptedException e) {
                        }
                    }            }
            }
        }    /**
         * the entrance of main program
         * 
         * @param args
         * @throws Throwable
         */
        public static void main(String[] args) throws Throwable {
            final ThreadPrintSample instance = new ThreadPrintSample();
            final Thread t1 = new PrintNumber(1, instance);
            final Thread t2 = new PrintNumber(0, instance);        t1.setName("Odd");
            t2.setName("Even");        t1.start();
            t2.start();        t1.join();
            t2.join();        System.out.println("over");
        }
    }
      

  3.   

    补充一点
    synchronized 同步的不是同一对象也是错误之一。 线程之间怎么传递消息?还有代码格式,看着变扭。