线程同步。以正确的方式打印值

时间:2013-04-06 04:58:40

标签: java multithreading

我无法找到此问题的解决方案。我在某些方面搞砸了,但不确定。请提出您的建议以克服这一点。

问题:

有三个数组,每个数组都分配给线程,输出应该按顺序排列......

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

预期产出

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

我尝试过:

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();
            }
        }
    }
}

和测试类

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();

    }
}

1 个答案:

答案 0 :(得分:4)

试试我的版本。请注意,应始终在循环中使用wait。

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();
        }
    }
}

输出

1
2
3
4
5
6
7
8
9