package hefan;
public class Hefan {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Chuangkou ck = new Chuangkou(100);
Thread t1 = new Thread(ck);
Thread t2 = new Thread(ck);
t1.start();
t2.start();
}
}
class Chuangkou implements Runnable{
int num ;
public Chuangkou(int num) {
super();
this.num = num;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
synchronized(this){
if(num <0){
break;
}
System.out.println("剩余"+ num);
num--;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
可以运行。挺好的题目。对线程的理解又高了点。。就感觉运行的时候语境有点偏new Chuangkou(100); |