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

标签 java multithreading

我想用 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

谁能帮帮我。

最佳答案

问题是:

  1. 设计存在线程之间引发条​​件的问题,需要同步它们。
  2. 使用构造函数时PrintSequenceRunnable(String tname, int a )您正在发送原始变量 a 的副本,这是一个静态成员 NumberGame 。所以,每个PrintSequenceRunnable有自己的变量a .

我的建议是使用方法 wait 同步每个线程和notify 。我拿了你的代码并做了一些修改:

数字游戏

public class NumberGame {

    public static void main(String args[]) throws InterruptedException
    {       
        PrintSequenceRunnable C1=new PrintSequenceRunnable("T1");
        PrintSequenceRunnable C2=new PrintSequenceRunnable("T2");
        PrintSequenceRunnable C3=new PrintSequenceRunnable("T3");

        Thread t1 = new Thread(C1);
        Thread t2 = new Thread(C2);
        Thread t3 = new Thread(C3);

        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(1);//Wait 1 ms to avoid a raise condition

        PrintSequenceRunnable.activateNextItem(); //Start sequence.


        t1.join();
        t2.join();
        t3.join();

        System.out.println("--END--");
    }
}

PrintSequenceRunnable

import java.util.Vector;

public class PrintSequenceRunnable implements Runnable {    

    static private int a = 0;
    private static Vector<PrintSequenceRunnable> items = new Vector<PrintSequenceRunnable>();

    /**
     * Method to select the next Thread which will be activate to continue its thread.
     */
    public static synchronized void activateNextItem() {
        int index = a % items.size();
        items.get(index).activate();
    }

    private String tname;
    private Object sempahoro = new Object(); //Object to sinchrony the  thread

    public PrintSequenceRunnable(String tname)
    {
        this.tname = tname;
        items.add(this);
    }

    public void activate()
    {
        synchronized (sempahoro) {
            sempahoro.notify();
        }
    }

    @Override
    public void run() {
        for(int i=0; i<10;i++)
        {

            synchronized (sempahoro) {
                try {
                    sempahoro.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            a++;
            System.out.println(tname+" "+a);            
            activateNextItem(); //Raise the next thread.
        }
        // TODO Auto-generated method stub
    }
}

在此示例中,方法 activateNextItem, from PrintSequenceRunnable`,将决定通知哪个实例执行其线程。

重要,我需要设置一个 sleep初始化每个线程后一秒以避免引发条件,我的意思是:WAITING所有线程启动并使所有线程处于等待状态。

输出:

T1 1
T2 2
T3 3
T1 4
T2 5
T3 6
T1 7
T2 8
T3 9
T1 10
T2 11
T3 12
T1 13
T2 14
T3 15
T1 16
T2 17
T3 18
T1 19
T2 20
T3 21
T1 22
T2 23
T3 24
T1 25
T2 26
T3 27
T1 28
T2 29
T3 30
--END--

关于java - 如何使用多线程打印序列中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57875711/

相关文章:

java - 在 Android Studio 中找不到符号

java - Thrift 服务与 RESTful 服务哪个更好?

java - 在 struts 1.2 中验证后保留表单值

c - 为什么使用 usleep 而不是 sleep

java - 每个 JVM 实例上开始执行多少线程?

java - 从表格 View 中选择列

java - POI - 无法在 osgi 中打开 xlsx 文件

python - 限制 Python 线程的并发和速率

cocoa - 如何创建只监听performSelector :onThread: and GUI events?的运行循环

java - 等待一个线程以.join不一致结束