基本上,它的作用是按顺序打印以下 2 和 3 的倍数
2 3 4 6 6 9 8 12 10 = this is the output
(2*1=2) (3*1=3) (2*2=4) (3*2=6) (2*3=6) (3*3=9) (2*4=8) (3*4=12) (2*5=10) = just a guide
这是到目前为止我的代码,我无法按顺序显示它。我尝试过使用等待和通知,但很困惑。到目前为止,这个正在运行。
public class Main {
public static void main(String[] args) throws InterruptedException {
final Thread mulof2 = new Thread(){
public void run() {
for (int i = 1; i <= 10; i++) {
int n = 2;
int result = n * i;
System.out.print(result + " ");
}
}
};
Thread mulof3 = new Thread(){
public void run() {
for (int i = 1; i <= 10; i++) {
int n = 3;
int result = n * i;
System.out.print(result + " ");
}
}
};
mulof2.start();
mulof3.start();
}
}
最佳答案
对于 Java 7,您的首选应该是 Phaser
。您只需要它的一个实例,该实例是使用 new Phaser(1)
创建的。您只需要两种协调方法:arrive
和 awaitAdvance
。
关于java - 使用java线程编写一个程序来打印以下序列2 3 4 6 6 9 8 12 10(序列中2和3的倍数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11709536/