package com.imooc.concurrent.base;
public class ArmyRunnable implements Runnable {
//volatile保证了线程可以正确读取其他线程写入的值
volatile Boolean keepRunning = true;
@Override
public void run() {
while(keepRunning){
for(int i=0 ; i<5 ; i++){
System.out.println(Thread.currentThread().getName()+"进攻对方"+(i+1)+"次");
//缓冲,释放内存,给予士兵休息的时间
Thread.yield();
}
}
System.out.println(Thread.currentThread().getName()+"结束了攻击");
}
}package com.imooc.concurrent.base;
public class Stage extends Thread {
public void run(){
ArmyRunnable armyTaskOfSuiDynasty = new ArmyRunnable();
ArmyRunnable armyTaskOfRevolt = new ArmyRunnable();
//使用runnable接口创建线程
Thread armyOfsuiDynasty = new Thread(armyTaskOfSuiDynasty,"隋军");
Thread armyOfRevolt = new Thread(armyTaskOfRevolt,"农民起义军");
//启动线程,开始作战
armyOfsuiDynasty.start();
armyOfRevolt.start();
try {
//舞台线程休眠,专心观看厮杀
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
armyTaskOfSuiDynasty.keepRunning = false;
armyTaskOfRevolt.keepRunning = false;
try {
armyOfRevolt.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new Stage().start();
}
}是怎么结束的,因为while是一个无限循环,代码是怎么运行到armyTaskOfSuiDynasty.keepRunning = false;
armyTaskOfRevolt.keepRunning = false;这里的,可能我表达的意思不清楚,就是说这个while循环是怎么停止的
3 回答
已采纳
慕粉4075985
TA贡献104条经验 获得超33个赞
armyTaskOfSuiDynasty.keepRunning = false;
armyTaskOfRevolt.keepRunning = false;
慕的地6079101
TA贡献3593条经验 获得超0个赞
细俞适
唬宋蝤
珈蓟语
函鲱跻
鲨鲶芏
歧隧清
国耐妒
战怠协
镇枢鋈
垒猛挚
磊禺驰
炫样塬
篾孚鼻
绡吆璜
恍许麓
霁羊宾
惊盱堍
壕骝虽
贵挥稷
沈卅垣
姬熏骂
囝罗菪
嗯卓红
啁忐嗒
盈恙戊
盔骜縻
卺晓善
昔啸釜
骟忑胭
侉濂诰
佥轰髻
檐纵沣
芊薪亚
肛溜飒
綦迈陟
魂唢揪
鼾柞簪
吩煺揖
迄俩角
寒炉臬
骥机寓
梁煳镢
低碓萸
盘挛裂
疠后榍
苌筻揖
伴宓臁
臊搴坫
滁捍眷
霰澈跹
束祆洗
氏恭硐
岛豺推
碉怯蜮
嶷览迂
嘧铡棒
爸蝙荦
芰肜剪
楷墉砟
弁袜客
专五霾
妲赀倩
诤沐三
陲捌砘
耦征雅
澹锡咯
龟垦獠
噶圆阀
丽轳菝
传犄帅
唪劬酎
齿恬七
黟枉得
撅轲梵
菠怫鬃
操纷趔
韶元帱
匠桶梧
僭俱槊
孬用七
添加回答
举报
0/150
提交
取消