使用python或者java语言 多线程输出1-100。
使用python或者java语言 多线程输出1-100。
python或者java 多线程输出1-100
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- CSDN专家-sinJack 2021-11-25 09:01关注
public class ThreadTest { volatile int flag=0; public void runThread() throws InterruptedException{ Thread t1=new Thread(new Thread1()); Thread t2=new Thread(new Thread2()); t1.start(); t2.start(); } public class Thread1 implements Runnable{ public void run() { int i=0; while(i<=99){ if(flag==0) { System.out.println("t1="+i+"flag="+flag); i+=2; flag=1; } } } } public class Thread2 implements Runnable{ public void run() { int i=1; while(i<=99){ if(flag==1) { System.out.println("t2="+i+"flag="+flag); i+=2; flag=0; } } } } }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报