Java线程,每秒打印1个字符,无顺序
我试图打印这些线程每秒1个字符,没有顺序。如果我进入睡眠状态,它就会变得井然有序。如何使其不按顺序并每秒打印1个字符(任意)Java线程,每秒打印1个字符,无顺序,java,multithreading,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"));
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);
}
}
}
公共类编号1{
公共静态void main(字符串[]args){
试一试{
螺纹a=新螺纹(新螺纹1(“a”));
螺纹b=新螺纹(新螺纹1(“b”));
螺纹c=新螺纹(新螺纹1(“c”));
a、 start();
b、 start();
c、 start();
a、 加入(1000);
b、 加入(1000);
c、 加入(1000);
}
捕捉(中断异常e){
System.out.println(“错误”);
}
}
}
类thread1实现可运行的{
字符串;
公共线程1(字符串a){
这个字符=a;
}
公开募捐{
for(inti=1;i我在代码中为for循环添加了一个sleep,它似乎正在工作
1号螺纹:C
1号螺纹:B
1号线程:A
2号螺纹:A
二号线:C
二号线:B
三号线:A
三号线:C
三号线:B
4号螺纹:C
4号螺纹:B
4号螺纹:A
5号螺纹:A
5号螺纹
螺纹:B
5号螺纹:C
字符A、B、C以随机顺序显示。这不是您想要实现的吗?Thread.sleep()是一种使线程休眠1秒并继续的方法。您正在使用方法join。因此将其更改为Thread.sleep(1000)
公共类编号1{
公共静态void main(字符串[]args){
试一试{
螺纹a=新螺纹(新螺纹1(“a”));
螺纹b=新螺纹(新螺纹1(“b”));
螺纹c=新螺纹(新螺纹1(“c”));
a、 start();
b、 start();
c、 start();
a、 加入(1000);
b、 加入(1000);
c、 加入(1000);
}
捕捉(中断异常e){
System.out.println(“错误”);
}
}
}
类thread1实现可运行的{
字符串;
公共线程1(字符串a){
这个字符=a;
}
公开募捐{
对于(inti=1;i我将使用信号量
我将信号量初始化为零许可证,并使三个计数线程中的每一个在循环中每次获得一个许可证。最后,我将拥有主线程循环,每秒钟向信号量添加一个许可证,直到所有线程都完成为止。如果我理解正确,您正在尝试创建所谓的race条件:打印的字符取决于首先到达的线程。因此,通常会同时打印三个字符,而不是一次打印一个。如果不打印,则顺序将不再是随机的。最好的方法是让三个线程先通过记住顺序来重新排列顺序其中,他们每个人都想打印一个字符,然后再次加入线程,然后从其中一个线程打印出三个字符。我绝对不是这方面的专家,但我认为您还需要将字符附加到字符串的代码放入同步块中。有点像这样:
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)
{
}
}
}
}
}
公共类编号1
{
公共静态void main(字符串[]args)
{
打印机=新打印机();
尝试
{
线程a=新线程(新线程1(“a”,打印机));
线程b=新线程(新线程1(“b”,打印机));
线程c=新线程(新线程1(“c”,打印机));
a、 start();
b、 start();
c、 start();
a、 加入(1000);
b、 加入(1000);
c、 加入(1000);
}
捕捉(中断异常e)
{
System.out.println(“错误”);
}
}
}
类打印机
{
int i=0;
字符串[]字符串=新字符串[3];
//此方法仅在三个线程中的最后一个线程调用它时打印并等待打印所有三个线程
//字符。因此,如果线程本身需要等待三秒钟,则返回true。所有三个
//线程必须尝试同时调用它,以便顺序是随机的;以防止方法同步时出错。
公共同步布尔打印(字符串s)
{
字符串[i++]=s;
如果(i==3)
{
对于(i=0;i),它每秒打印三个字符。我希望它打印一个字符(线程)每秒的随机顺序。如果您注意到它在一段时间后打印时,顺序又是相同的。这种情况的发生是因为您一次启动三个线程。只需删除其他两个线程。然后它一次只打印一个字符。是的,我这样做并删除了。join()然后它每秒打印三个字符。我希望它以随机顺序每秒打印一个字符(线程)。
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)
{
}
}
}
}
}