Java线程,每秒打印1个字符,没有订单

时间:2016-03-27 08:16:04

标签: java multithreading

我试图在没有订单的情况下每秒打印这些线程1个字符。如果我进入睡眠状态,它就会变得有序。如何使其不按顺序排列并每秒打印1个字符(任意)。

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

        try{
        Thread a = new Thread(new thread1("A"));
        Thread b = new Thread(new thread1("B"));
        Thread c = new Thread(new thread1("C"));
        a.start();
        b.start();
        c.start();
        a.join(1000);
        b.join(1000);
        c.join(1000);
        }
        catch(InterruptedException e) {
                System.out.println("Error");
            }


    }
}

class thread1 implements Runnable{

    String character;


    public thread1(String a){
        this.character = a;
    }

    public void run(){
            for(int i = 1;i<21;i++)
            {           
            System.out.println("No."+i+" Thread: "+character);
            }
    }
}

4 个答案:

答案 0 :(得分:0)

我在你的代码中为for循环添加了一个sleep,它似乎正在工作。

  

No.1螺纹:C
  No.1螺纹:B
  No.1螺纹:A
  No.2线程:A
  No.2螺纹:C
No.2螺纹:B
No.3螺纹:A
No.3螺纹:C
No.3螺纹:B
  4号螺纹:C
形4号螺纹:B
形4号螺纹:A
No.5螺纹:A
No.5   螺纹:B
第5螺纹:C

字符A,B,C以随机顺序显示。这不是你想要达到的目标吗?

答案 1 :(得分:0)

Thread.sleep()是一种让你的线程休眠1秒并继续的方法。您正在使用方法连接。所以将它改为thread.sleep(1000

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

        try{
        Thread a = new Thread(new thread1("A"));
        Thread b = new Thread(new thread1("B"));
        Thread c = new Thread(new thread1("C"));
        a.start();
        b.start();
        c.start();
        a.join(1000);
        b.join(1000);
        c.join(1000);
        }
        catch(InterruptedException e) {
                System.out.println("Error");
            }


    }
}

class thread1 implements Runnable{

    String character;


    public thread1(String a){
        this.character = a;
    }

    public void run(){
            for(int i = 1;i<21;i++)
             {           
               Thread.sleep(1000);// This will make the current thread sleep for 1 second  
           System.out.println("No."+i+" Thread: "+character);
            }
    }
}

答案 2 :(得分:0)

我会使用信号量。

我将信号量初始化为零许可,并且我会使每个三个计数线程每次绕循环获取一个许可。最后,我有主线程循环,向信号量添加一个许可证,直到所有线程都完成。

答案 3 :(得分:0)

如果我理解正确你正在尝试创建所谓的竞争条件:打印的字符取决于首先到达那里的线程。现在,因为这是正常的,三个字符同时打印而不是一次打印。如果没有,订单将不再是随机的。最好的方法是让三个线程首先通过记住每个线程想要打印字符的顺序来重新排序,然后再次连接线程然后从其中一个线程打印出三个字符。我不是这方面的专家,但我认为你需要将在字符串中附加字符的代码放在同步块中。有点像这样:

public class Number1
{
    public static void main(String[] args)
    {
        Printer printer=new Printer();
        try
        {
            Thread a = new Thread(new thread1("A", printer));
            Thread b = new Thread(new thread1("B", printer));
            Thread c = new Thread(new thread1("C", printer));
            a.start();
            b.start();
            c.start();
            a.join(1000);
            b.join(1000);
            c.join(1000);
        }
        catch(InterruptedException e)
        {
                System.out.println("Error");
        }
    }
}

class Printer
{
    int i=0;
    String[] strings=new String[3];
    //this method only prints and waits if it is called by the last of the three threads, to print all three
    //characters. So it returns true if the thread itself needs to wait three seconds. All three
    //threads must try to call it simultaneously so the order is random; to prevent errors though the method is synchronised.
    public synchronized boolean print(String s)
    {
        strings[i++]=s;
        if(i==3)
        {
            for(i=0; i<3; i++)
            {
                System.out.println(strings[i]);
                try
                {
                    Thread.sleep(1000);
                }
                catch(Exception e)
                {
                }
            }
            i=0;
            return false;
        }
        return true;
    }
}

class thread1 implements Runnable
{
    String character;
    Printer printer;
    public thread1(String a, Printer printer)
    {
        this.character = a;
        this.printer=printer;
    }

    public void run()
    {
        for(int i = 1;i<21;i++)
        {    
            if(printer.print("No."+i+" Thread: "+character))
            {
                try
                {
                    Thread.sleep(3000);
                }
                catch(Exception e)
                {
                }
            }
        }
    }
}
相关问题