简体   繁体   中英

Thread Synchronization. Printing the values in right way

I am not able to find solution of this problem. I am messing at some point, but not sure. Kindly give your suggestions to overcome this.

Problem:

Have three arrays, each array is assigned to thread, output should be in sequence...

t1 ={1,4,7} t2 ={2,5,8} t3 ={3,6,9}

expected output

out = {1,2,3,4,5,6,7,8,9}

Wht i tried:

public class Worker extends Thread {
    Worker next;
    int[] val;
    Object lock = new Object();

    Worker(int[] val) {

        this.val = val;
    }

    public void setnext(Worker next) {
        this.next = next;
    }

    @Override
    public void run() {
        for (int i = 0; i < val.length; i++) {
            synchronized (this) {
                synchronized (next) {

                    System.out.println(val[i]);
                    next.notify();

                }

                synchronized (this) {
                    try {

                        this.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            synchronized(next){
                next.notify();
            }
        }
    }
}

and test class

public class TestWorker {
    public static void main(String[] args) throws Exception{
        Worker worker1 = new Worker(new int[]{1,4,7});
        Worker worker2 = new Worker(new int[]{2,5,8});
        Worker worker3 = new Worker(new int[]{3,6,9});

        worker1.setnext(worker3);
        worker2.setnext(worker1);
        worker3.setnext(worker2);

        worker1.start();
        worker2.start();
        worker3.start();

    }
}

Try my version. Note that wait should always be used in a loop.

public class Worker extends Thread {
    int[] val;
    Worker next;
    boolean ready;
    boolean go;

    Worker(int[] val) {
        this.val = val;
    }

    public void setNext(Worker next) {
        this.next = next;
    }

    @Override
    public void run() {
        synchronized (Worker.class) {
            ready = true;
            Worker.class.notifyAll();
            for (int i = 0; i < val.length; i++) {
                try {
                    while (!go) {
                        Worker.class.wait();
                    }
                    System.out.println(val[i]);
                    go = false;
                    next.go = true;
                    Worker.class.notifyAll();
                } catch (InterruptedException e) {
                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
        Worker worker1 = new Worker(new int[] { 1, 4, 7 });
        Worker worker2 = new Worker(new int[] { 2, 5, 8 });
        Worker worker3 = new Worker(new int[] { 3, 6, 9 });

        worker1.setNext(worker2);
        worker2.setNext(worker3);
        worker3.setNext(worker1);

        worker1.start();
        worker2.start();
        worker3.start();

        synchronized (Worker.class) {
            while(!worker1.ready || !worker2.ready ||!worker3.ready ) {
                Worker.class.wait();
            }
            worker1.go = true;
            Worker.class.notifyAll();
        }
    }
}

output

1
2
3
4
5
6
7
8
9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM