gpt4 book ai didi

java - java中的多线程使用3个线程打印abc

转载 作者:行者123 更新时间:2023-12-01 20:52:02 24 4
gpt4 key购买 nike

任务是使用三个线程无限次重复打印 abc我的代码是

package javap;

public class Pattern {

volatile int status=1;
public static void main(String[] args) {

Pattern p = new Pattern();

A1 a=new A1(p);
B1 b=new B1(p);
C1 c=new C1(p);

a.start();
b.start();
c.start();
}
}

class A1 extends Thread{
Pattern p1;

A1(Pattern p){
this.p1 = p;
}

@Override
public void run() {

try{
synchronized (p1) {

for (int i = 0; i < 100; i++) {

while(p1.status!=1){
p1.wait();
}

System.out.print("A ");
p1.status = 2;
p1.notifyAll();
}

}
}catch (Exception e) {
System.out.println("Exception 1 :"+e.getMessage());
}

}

}

class B1 extends Thread{

Pattern p2;

B1(Pattern p2){
this.p2 = p2;
}

@Override
public void run() {

try{
synchronized (p2) {

for (int i = 0; i < 100; i++) {

while(p2.status!=2){
p2.wait();
}

System.out.print("B ");
p2.status = 3;
p2.notifyAll();
}

}
}catch (Exception e) {
System.out.println("Exception 2 :"+e.getMessage());
}

}
}


class C1 extends Thread{

Pattern p3;

C1(Pattern p){
this.p3 = p;
}

@Override
public void run() {

try{
synchronized (p3) {

for (int i = 0; i < 100; i++) {

while(p3.status!=3){
p3.wait();
}

System.out.print("C ");
p3.status = 1;
p3.notifyAll();
}

}
}catch (Exception e) {
System.out.println("Exception 3 :"+e.getMessage());
}

}
}

当我尝试使用 for(;;) 或 while(true) 时,我的 ide 挂起并且没有得到任何输出所以我把它限制在100次。无论如何,我可以让它运行无限次。

提前致谢

最佳答案

你的代码看起来不错。也就是说,您似乎编写了太多代码来实现您的目标...考虑使用 Executors.newFixedThreadPool(3) 创建 3 个线程并将它们放入池中。

ExecutorService service = Executors.newFixedThreadPool(3);

然后,将任务传递到池中(可调用)

Future<String> resultA = service.submit(() -> {
System.out.print("A ");
return "A";
});

现在等待任务完成,然后再传递下一个任务:

resultA.get();

这是完整的代码片段:

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

ExecutorService service = Executors.newFixedThreadPool(3);
int x = 0;
while (x < 100) {
service.submit(() -> {
System.out.print("A ");
return "A";
}).get();

service.submit(() -> {
System.out.print("B ");
return "B";
}).get();

service.submit(() -> {
System.out.print("C ");
return "C";
}).get();

x++;
}

service.shutdown();
}

也就是说,您可以使用以下方法删除所有重复代码:

public class Main {
public static void main(String[] args) throws ExecutionException, InterruptedException {

ExecutorService service = Executors.newFixedThreadPool(3);
int x = 0;
while (x < 100) {
service.submit(getTask("A ")).get();
service.submit(getTask("B ")).get();
service.submit(getTask("C ")).get();
x++;
}

service.shutdown();
}

private static Callable<String> getTask(String task) {
return () -> {
System.out.print(task);
return task;
};
}
}

请注意,main 不得抛出异常,但这应该是显而易见的。

此外,@Elliott Frisch 的答案非常棒,因为它使用了 JAVA 8 并行流。
我认为您正在尝试了解线程,因此我的答案使用了 Executors API,这使得与线程相关的代码更加简短。

应该熟悉 Streams 和 Executors。

关于java - java中的多线程使用3个线程打印abc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43176198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com