如何安排三个线程作用于单个对象?

时间:2013-08-28 07:38:52

标签: java multithreading synchronization


我有一个要求, 3个线程作用于sinlge对象,该对象有三个方法,每个线程都有一个方法,比如..

T1 ---> object.printOne(int number)。
T2 ---> object.printTwo(int number)。
T3 ---> object.printThree(int number)。

现在我想让这些线程同步,并且对象访问应该按照

这样的顺序

t1打印....值
t2打印....值
t3打印....值
t1打印....值
t2打印....值
t3打印....值
t1打印....值
t2打印....值
t3打印....值
....等等......

我有以下代码..
建议我任何技术

One.java

public class One extends Thread {

    PrintNumbers p;
    int i = 1;

    public One(PrintNumbers p) {
        this.p = p;
    }

    @Override
    public void run() {

        int prev = 1;
        while (prev < 1111) {
            p.printOne(prev);
            prev = (int) (prev +  Math.pow(10, i));
            i = i + 1;
        }

    }
}

Two.java

public class Two extends Thread {

    int i = 1;

    PrintNumbers p;

    public Two(PrintNumbers p) {
        this.p = p;

    }

    @Override
    public void run() {

        int prev = 2;
        while (prev < 2222) {
            p.printTwo(prev);
            prev = (int) (prev + 2 * Math.pow(10, i));
            i = i + 1;
        }

    }
}

Three.java

public class Three extends Thread {

    PrintNumbers p;
    int i = 1;

    public Three(PrintNumbers p) {
        this.p = p;
    }

    @Override
    public void run() {

        int prev = 3;
        while (prev < 3333) {
            p.printThree(prev);
            prev = (int) (prev + 3 * Math.pow(10, i));
            i = i + 1;
        }

    }
}

PrintNumbers.java

import java.util.logging.Level;
import java.util.logging.Logger;

public class PrintNumbers {

    public enum status {
        A, B, C
    }

    status X = status.A;
    //
    public synchronized void printOne(int number) {
        //
        if (!X.equals(status.A) && !Y.equals(status.B)) {

            try {
                wait();

            } catch (InterruptedException ex) {
                Logger.getLogger(PrintNumbers.class.getName()).log(
                        Level.SEVERE, null, ex);
            }
        }
        System.out.println(Thread.currentThread().getName() + number);
        X = status.B;

        notifyAll();

    }

    public synchronized void printTwo(int number) {
        //
        if (!X.equals(status.B) && !Y.equals(status.C)) {

            try {
                // System.out.println("under wait even");
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(PrintNumbers.class.getName()).log(
                        Level.SEVERE, null, ex);
            }
        }
        System.out.println(Thread.currentThread().getName() + number);
        X = status.C;

        notifyAll();

    }

    public synchronized void printThree(int number) {
        //
         if (!X.equals(status.C) && !Y.equals(status.A)) {

            try {
                // System.out.println("under wait even");
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(PrintNumbers.class.getName()).log(
                        Level.SEVERE, null, ex);
            }
        }
        System.out.println(Thread.currentThread().getName() + number);
        X = status.A;


        notifyAll();

    }
    //
}

PrintTest.java

public class PrintTest {

    public static void main(String[] args) {

        PrintNumbers b = new PrintNumbers();

        One firstThread = new One(b);
        Two secondThread = new Two(b);
        Three thirdThread = new Three(b);

        secondThread.setName("second: ");
        thirdThread.setName("third: ");
        firstThread.setName("first: ");

        firstThread.start();
        secondThread.start();
        thirdThread.start();
    }
}

O / P:
第一:1 第二名:2
第三名:3
第一名:11
第二名:22
第三名:33
第一名:111
第二名:222
第三名:333
......等等

1 个答案:

答案 0 :(得分:0)

将班级if中的PrintNumbers块更改为while以获得所需的行为

if (!X.equals(status.A) && !Y.equals(status.B))

while (!X.equals(status.A))

同时删除未使用的Y条件。