1


我有一个要求,3个线程正在作用于一个对象,并且该对象具有三个方法,每个线程都有一个方法,例如..

t1---> object.printOne(int number)。
t2---> object.printTwo(int number)。
t3---> object.printThree(int number)。

现在我想让这些线程同步,并且对象访问的顺序应该是

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

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

一.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;
        }

    }
}

二.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;
        }

    }
}

三.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
...依此类推

4

1 回答 1

0

更改if课堂上的块PrintNumberswhile获得所需的行为

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

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

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

于 2013-08-28T12:14:45.620 回答