本帖最后由 zl78365336 于 2013-11-10 17:58 编辑
请问各位师兄:
《卖票的题》平常的方式:100张票售卖是正常的。
//<??可是用匿名内部类的方式却是售卖了400张票,麻烦各位师兄给点指导,谢谢!>
class Ticket implements Runnable{
private int tick = 100;
public void run(){
while(true){
if(tick>0)
System.out.println(Thread.currentThread().getName()+" sale: "+tick--);
}
}
}
public class TicketTest {
public static void main(String[] args) {
Ticket t = new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();*/
}
}
//用匿名内部类的方式
public class TicketTest {
public static void main(String[] args) {
Runnable t = new Runnable(){
public void run(){
int tick = 100;
while(true){
if(tick>0)
System.out.println(Thread.currentThread().getName()+" zz_sale: "+tick--);
}
}
};
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
|