初识并发问题
模拟抢票
public class TestThread4 implements Runnable{
private int ticketNum = 100;
@Override
public void run() {
while (true){
if(ticketNum<=0){
break;
}
System.out.println(Thread.currentThread().getName()+"拿到了第"+ticketNum--+"张票");
}
}
public static void main(String[] args) {
TestThread4 testThread4 = new TestThread4();
new Thread(testThread4,"小明").start();
new Thread(testThread4,"小红").start();
}
}
其中Thread类中currentThread(),为获取当前线程然后里边getname方法是获得当前线程名