180个针对Java初学者的简单实例,包含了180個適合與初學者的源碼實例
源代码在线查看: consumer.java
class Consumer extends Thread
{
int nTime;
String strConsumer;
int count;
public Consumer(int nTime, String strConsumer){
this.nTime = nTime;
this.strConsumer = strConsumer;
count=0;
}
public void run(){
while(true){
try{
System.out.println("消费者姓名:"+strConsumer+" .");
Thread.sleep(nTime);
} catch(Exception e) {
e.printStackTrace();
}
if(count++>3) break;
}
}
static public void main(String args[]){
Consumer aConsumer = new Consumer (1000, "aConsumer");
aConsumer.start();
Consumer bConsumer = new Consumer (2000, "bConsumer");
bConsumer.start();
Consumer cConsumer = new Consumer (3000, "cConsumer ");
cConsumer.start();
}
}