java线程打印A到z_一个线程打印A,一个线程打印B,一个线程打印C,实现循环打印ABC 10次。主函数有个小地方不懂!...

/*功能:三个线程,一个打印A,一个打印B,一个打印C,主函数实现循环打印ABCABCABCABC... 共打印10次ABC*/

public class ThreadABC {

public static void main(String[] args) {

Print p = new Print();

/*打印A的进程*/

new Thread(new Runnable() {

public void run() {

for(int i = 0;i <10;++i){//我设置的i打印10次啊

p.print_A();

/*为什么只打印 5 次???!!!*/

/*加上下面这行代码,就能正常打印10次,why???*/

//try{Thread.sleep();}catch(Exception e){;}

}

}

}).start();

/*启动打印B的线程*/

new Thread(new Runnable() {

public void run() {

while(true)

p.print_B();

}

}).start();

/*启动打印C的线程*/

new Thread(new Runnable() {

public void run() {

while(true)

p.print_C();

}

}).start();

}

}

/**/

class Print {

/*flag用来同步*/

private int flag = 1;

/*打印 A */

synchronized void print_A() {

if(flag == 1) {

System.out.print("A");

flag = 2;

try {this.notifyAll();}catch(Exception e) {e.printStackTrace();}

}

else {

try {this.wait();}catch(Exception e) {e.printStackTrace();}

}

}

/*打印B*/

synchronized void print_B() {

if(flag == 2) {

System.out.print("B");

flag = 3;

try {this.notifyAll();}catch(Exception e) {e.printStackTrace();}

}

else {

try {this.wait();}catch(Exception e) {e.printStackTrace();}

}

}

/*打印C*/

synchronized void print_C() {

if(flag == 3) {

System.out.println("C");

flag = 1;

try {this.notifyAll();}catch(Exception e) {e.printStackTrace();}

}

else {

try {this.wait();}catch(Exception e) {e.printStackTrace();}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以帮你编写一个Java程序,用三个线程循环打印a、b、c。以下是一个简单的实现示例: ```java class PrintThread implements Runnable { private static final Object lock = new Object(); private static volatile char currentChar = 'a'; private char printChar; public PrintThread(char printChar) { this.printChar = printChar; } @Override public void run() { for (int i = 0; i < 10; i++) { synchronized (lock) { while (currentChar != printChar) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.print(printChar); if (currentChar == 'c') { System.out.println(); // 换行 } currentChar = (char) (currentChar + 1 > 'c' ? 'a' : currentChar + 1); lock.notifyAll(); } } } } public class Main { public static void main(String[] args) { Thread threadA = new Thread(new PrintThread('a')); Thread threadB = new Thread(new PrintThread('b')); Thread threadC = new Thread(new PrintThread('c')); threadA.start(); threadB.start(); threadC.start(); } } ``` 上述代码中,我们定义了一个名为`PrintThread`的线程类,它实现了`Runnable`接口。每个线程都会打印一个特定的字符,共循环打印10。通过`synchronized`关键字和`wait()`、`notifyAll()`方法来实现线程间的同步和互斥。在`run()`方法中,每个线程会判断当前应该打印的字符是否为自己负责打印的字符,如果不是,则线程进入等待状态。当轮到自己打印时,输出相应的字符,并使用`notifyAll()`方法唤醒其他线程。最后,主函数中创建三个线程,并启动它们。 运行以上代码,你会看到输出结果为: ``` abc abc abc ... ``` 希望这能帮到你!如果你还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值