0

I am trying to use 2 threads. 1 thread prints only odd number and the other thread prints only even number and It has to be an alternative operation.

Eg:

Thread1 1
Thread2 2
Thread1 3
Thread2 4
and so on..

Below is the program, please let me know where I am going wrong as the thread1 is not coming out of wait state even when the thread2 is notifying it..

    public class ThreadInteraction {

    public static void main(String[] args) {
        new ThreadInteraction().test();
    }

    private void test() {
        ThreadA ta = new ThreadA();
        Thread t = new Thread(ta);
        t.start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        for(int i=2;i<=50;){
            System.out.println("Thread2 "+i);
            synchronized (t) {
                try {
                    t.notify(); 
                    t.wait();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            i=i+2;
        }
    }
}

    class ThreadA implements Runnable{
        @Override
        public void run() {
            for(int i=1;i<50;){
                System.out.println("Thread1 "+i);
                synchronized (this) {
                        try {
                            notify();                           
                            wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                }
                i=i+2;
            }
        }
    }
4

3 回答 3

2

问题是,在一种情况下,您正在锁定Thread t[synchronized (t)],而在另一种情况下,您正在锁定TheadA对象本身 [synchronized(this)]。

如果您希望线程相互交谈,那么两者都应该锁定同一个对象,然后等待通知将按您的预期工作。

编辑:

您的程序中还有另一个问题,您没有使用任何变量在 2 个线程之间进行协调。所以你可能会看到这样的输出 2,1,4,3...等等。重点是线程将交替工作,但不是按顺序工作。因此,您应该在 2 个线程之间共享一个变量,该变量应该递增。第二个问题是您没有处理虚假的唤醒呼叫[阅读有关此的一些文档],您应该始终在 while 循环中调用 wait 。

于 2013-10-06T07:20:27.250 回答
0

根据 Lokesh 提供的答案修改了我的代码

public class ThreadInteraction {
    public static void main(String[] args) {
        new ThreadInteraction().test();
    }
    private void test() {
        ThreadA ta = new ThreadA();
        Thread t = new Thread(ta);
        t.start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        for(int i=2;i<=50;){
            System.out.println("Thread2 "+i);
            synchronized (ta) {
                try {
                    ta.notify();    
                    ta.wait();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            i=i+2;
        }
    }
}
class ThreadA implements Runnable{
    @Override
    public void run() {
        for(int i=1;i<50;){
            System.out.println("Thread1 "+i);
            synchronized (this) {
                    try {
                        notify();                           
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
            }
            i=i+2;
        }
    }
}
于 2013-10-06T07:24:53.567 回答
0

您对线程和锁有真正的困惑。我建议您创建一个且唯一一个用于锁定的对象,因为您似乎并不清楚您要锁定什么。

如果您 notify() 并且没有人在听,则信号丢失。但是,wait() 可能会虚假唤醒。

出于这个原因,一个 notify() 应该伴随着一个状态变化,一个 wait() 应该在一个循环中检查这个变化。

于 2013-10-06T08:07:07.903 回答