meexplorer February 2016

Printing values from different threads using wait and notify


I have three threads ThreadA, ThreadB and ThreadC printing values A, B and C respectively in loop.
I want output to be like A,B,C and then again A, B and C till loops are executing in threads.I want to write this sample program using wait and notify. Below code is printing the desired output but sometimes I am just seeing "A" in output, I am not able to figure out the case.

public class ThreadOrder {

    public static void main(String[] args) {
        Object lockAB = new Object();
        Object lockBC = new Object();
        Object lockCA = new Object();

        Thread threadA = new Thread(new ThreadOrder().new ThreadA(lockAB, lockCA));
        Thread threadB = new Thread(new ThreadOrder().new ThreadB(lockAB, lockBC));
        Thread threadC = new Thread(new ThreadOrder().new ThreadC(lockBC, lockCA));

        threadA.start();
        threadB.start();
        threadC.start();
    }

    class ThreadA implements Runnable {
        Object lockAB;
        Object lockCA;

        public ThreadA(Object lockAB, Object lockCA) {
            this.lockAB = lockAB;
            this.lockCA = lockCA;
        }

        @Override
        public void run() {
            for(int i=0; i<3; i++) {
                if(i!=0) {
                    try {
                        synchronized (lockCA) {
                            lockCA.wait();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("A");
                synchronized (lockAB) {
                    lockAB.notify();
                }
            }
        }

    }

    class ThreadB implements Runnable {
        Object lockAB;
        Object lockBC;

        public ThreadB(Object lockAB, Object lockBC) {
            this.lockAB = lockAB;
            this.lockBC = lockBC;
        }

        @Override
        public void run() {
            for(int         

Answers


james large February 2016

Consider creating a ring of threads connected to one another by blocking queues. Then you can pass a token around the ring. Each thread waits to receive the token, prints its output, passes the token on to the next thread in the ring, and goes back to waiting.


David Schwartz February 2016

You call wait, but you haven't tested if there's anything to wait for. You call notify, but you haven't changed anything that you would need to notify another thread about. You have all these synchronized methods, but no shared state for the synchronization to protect.

Nothing in your code makes any sense and it seems that you fundamentally don't understand what the wait/notify mechanism does. The wait function allows a thread to wait for some shared state to change, and the notify function allows one thread to tell others that some shared state has changed. But there has to be some shared state because the wait/notify mechanism (unlike a lock or sempahore) is internally stateless.

You should probably have some shared state protected by the synchronization. It should encode which thread should go next. If you need to print, but the shared state says it's not your turn, then you have something to wait for. When you print and make it some other thread's turn to print next, then you have something to notify other threads about.

Post Status

Asked in February 2016
Viewed 1,354 times
Voted 14
Answered 2 times

Search




Leave an answer