如何使用多线程打印序列中的数字

我想用 T1-1、T2-2、T3-3、T1-4、T2-5、T3-6 等线程按顺序打印数字


public class NumberGame {

    static int a=1;

    public static void main(String args[]) throws InterruptedException

    {

        PrintSequenceRunnable C1=new PrintSequenceRunnable("T1",a);

        PrintSequenceRunnable C2=new PrintSequenceRunnable("T2",a);

        PrintSequenceRunnable C3=new PrintSequenceRunnable("T3",a);


        Thread t1 = new Thread(C1);


        Thread t2 = new Thread(C2);


        Thread t3 = new Thread(C3);



        t1.start();


        t2.start();


        t3.start(); 


    }

}




public class PrintSequenceRunnable implements Runnable {

    String tname;

    int a;


    PrintSequenceRunnable(String tname, int a )

    {

        this.tname = tname;

        this.a = a;


    }


    @Override

    public void run() {


        synchronized (this) {

            for(int i=0; i<10;i++)

            {

                System.out.println(tname+" "+a);

                a++;

                try {

                    this.wait(1000);

                    this.notify();

                } catch (InterruptedException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                }


            }

        }


        // TODO Auto-generated method stub


    }


}

但我的输出就像


T1-1 T2-1 T3-1 T1-2 T3-2 T2-2 T3-3 T1-3 T2-3 T3-4 T1-4 T2-4 T3-5 T1-5 T2-5 T3-6 T1- 6 T2-6 T1-7 T2-7 T3-7 T2-8 T3-8 T1-8 T2-9 T3-9 T1-9 T2-10 T1-10 T3-10


谁能帮我。


智慧大石
浏览 115回答 2
2回答

慕田峪7331174

问题是:该设计存在线程之间引发条件的问题,您需要同步它们。在使用构造函数时,PrintSequenceRunnable(String tname, int a )您将发送原始变量的副本a,它是静态成员NumberGame。所以,每个PrintSequenceRunnable都有自己的变量a。我的建议是使用方法wait和同步每个线程notify。我拿了你的代码并做了一些修改:数字游戏public class NumberGame {&nbsp; &nbsp; public static void main(String args[]) throws InterruptedException&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; PrintSequenceRunnable C1=new PrintSequenceRunnable("T1");&nbsp; &nbsp; &nbsp; &nbsp; PrintSequenceRunnable C2=new PrintSequenceRunnable("T2");&nbsp; &nbsp; &nbsp; &nbsp; PrintSequenceRunnable C3=new PrintSequenceRunnable("T3");&nbsp; &nbsp; &nbsp; &nbsp; Thread t1 = new Thread(C1);&nbsp; &nbsp; &nbsp; &nbsp; Thread t2 = new Thread(C2);&nbsp; &nbsp; &nbsp; &nbsp; Thread t3 = new Thread(C3);&nbsp; &nbsp; &nbsp; &nbsp; t1.start();&nbsp; &nbsp; &nbsp; &nbsp; t2.start();&nbsp; &nbsp; &nbsp; &nbsp; t3.start();&nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(1);//Wait 1 ms to avoid a raise condition&nbsp; &nbsp; &nbsp; &nbsp; PrintSequenceRunnable.activateNextItem(); //Start sequence.&nbsp; &nbsp; &nbsp; &nbsp; t1.join();&nbsp; &nbsp; &nbsp; &nbsp; t2.join();&nbsp; &nbsp; &nbsp; &nbsp; t3.join();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("--END--");&nbsp; &nbsp; }}打印序列可运行import java.util.Vector;public class PrintSequenceRunnable implements Runnable {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; static private int a = 0;&nbsp; &nbsp; private static Vector<PrintSequenceRunnable> items = new Vector<PrintSequenceRunnable>();&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Method to select the next Thread which will be activate to continue its thread.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static synchronized void activateNextItem() {&nbsp; &nbsp; &nbsp; &nbsp; int index = a % items.size();&nbsp; &nbsp; &nbsp; &nbsp; items.get(index).activate();&nbsp; &nbsp; }&nbsp; &nbsp; private String tname;&nbsp; &nbsp; private Object sempahoro = new Object(); //Object to sinchrony the&nbsp; thread&nbsp; &nbsp; public PrintSequenceRunnable(String tname)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.tname = tname;&nbsp; &nbsp; &nbsp; &nbsp; items.add(this);&nbsp; &nbsp; }&nbsp; &nbsp; public void activate()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; synchronized (sempahoro) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sempahoro.notify();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0; i<10;i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; synchronized (sempahoro) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sempahoro.wait();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(tname+" "+a);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activateNextItem(); //Raise the next thread.&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; }}在此示例中,方法activateNextItem, fromPrintSequenceRunnable` 将决定通知哪个实例执行其线程。重要的是,我需要sleep在初始化每个线程后设置一秒以避免引发条件,我的意思是:等待所有线程启动并使所有线程处于等待状态。输出:T1 1T2 2T3 3T1 4T2 5T3 6T1 7T2 8T3 9T1 10T2 11T3 12T1 13T2 14T3 15T1 16T2 17T3 18T1 19T2 20T3 21T1 22T2 23T3 24T1 25T2 26T3 27T1 28T2 29T3 30--END--

慕少森

只是为了学习,这就是强制顺序输出的方法。但请注意,不能以任何方式保证线程的顺序执行。正如其他人指出的那样,这个问题不适合多线程处理。如果您想按顺序执行某件事,请在一个线程中执行。public class NumberGame {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; PrintSequenceRunnable.startFrom("T1");&nbsp; &nbsp; &nbsp; &nbsp; new Thread(new PrintSequenceRunnable("T1", "T2")).start();&nbsp; &nbsp; &nbsp; &nbsp; new Thread(new PrintSequenceRunnable("T2", "T3")).start();&nbsp; &nbsp; &nbsp; &nbsp; new Thread(new PrintSequenceRunnable("T3", "T1")).start();&nbsp; &nbsp; }}class PrintSequenceRunnable implements Runnable {&nbsp; &nbsp; private final String name;&nbsp; &nbsp; private final String next;&nbsp; &nbsp; private static String moveTo;&nbsp; &nbsp; private static int value = 1;&nbsp; &nbsp; PrintSequenceRunnable(String name, String next) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.next = next;&nbsp; &nbsp; }&nbsp; &nbsp; static void startFrom(String start) {&nbsp; &nbsp; &nbsp; &nbsp; moveTo = start;&nbsp; &nbsp; }&nbsp; &nbsp; private int uselessCounter = 0;&nbsp; &nbsp; @Override&nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; synchronized (moveTo) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (name.equals(moveTo)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(name + "-" + (value++));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; moveTo = next;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uselessCounter++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } while (value < 10);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Ran " + name + " uselessly for " + uselessCounter + " times."); // remove it.&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java