如何使用多线程在序列中打印数字 [英] How to print number in Sequence using multithreading

查看:101
本文介绍了如何使用多线程在序列中打印数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用T1-1,T2-2,T3-3,T1-4,T2-5,T3-6等线程依次打印数字

I Want to print number in sequence with thread like 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 1-7 T2-7 T3-7 T2-8 T3-8 1-8 T2-9 T3-9 1-9 T2-10 1-10 T3-10

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

任何人都可以帮助我.

推荐答案

出于学习的目的 ,这是强制执行顺序输出的方法.但是请注意,不能以任何方式保证顺序执行线程.正如其他人指出的那样,此问题不是多线程的理想选择.如果要顺序执行某项操作,请在一个线程中执行.

Just for the sake of learning, this is how you can enforce sequential output. But note that sequential execution of the threads is not guaranteed in any way. As others have pointed out that this problem is not a good candidate for multithreading. If you want to do something sequentially, do it in one thread.

public class NumberGame {
    public static void main(String[] args) {

        PrintSequenceRunnable.startFrom("T1");

        new Thread(new PrintSequenceRunnable("T1", "T2")).start();
        new Thread(new PrintSequenceRunnable("T2", "T3")).start();
        new Thread(new PrintSequenceRunnable("T3", "T1")).start();
    }
}

class PrintSequenceRunnable implements Runnable {

    private final String name;
    private final String next;

    private static String moveTo;

    private static int value = 1;

    PrintSequenceRunnable(String name, String next) {
        this.name = name;
        this.next = next;
    }

    static void startFrom(String start) {
        moveTo = start;
    }

    private int uselessCounter = 0;

    @Override
    public void run() {
        do {
            synchronized (moveTo) {
                if (name.equals(moveTo)) {
                    System.out.println(name + "-" + (value++));
                    moveTo = next;
                } else {
                    uselessCounter++;
                }
            }
        } while (value < 10);
        System.out.println("Ran " + name + " uselessly for " + uselessCounter + " times."); // remove it.
    }

}

这篇关于如何使用多线程在序列中打印数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆