java,
分享于 点击 11012 次 点评:146
java,
class VolatileCase extends Thread{//if some thread crash,all the threads exit
private volatile boolean crash = false;
//when count is equals 100 exit
private AtomicInteger count = new AtomicInteger(0);
@Override
public void run() {
while(true){
if(crash){
System.out.println(Thread.currentThread().getId()+" exit");
return;
}else{
count.incrementAndGet();
if(count.get() == 100){
//System.out.println(Thread.currentThread().getId() + " reach 100");
crash = true;
}
}
}
}
}
public class Thread2_4 {
public static void main(String[] args) throws InterruptedException {
for(int i=0;i<10;i++){
VolatileCase vo = new VolatileCase();
vo.start();
}
}
}
相关文章
- 暂无相关文章
用户点评