本篇实现的是模拟多线程获取同一资源
package lesson04;
/**
* 模拟多线程 获取同一资源
*/
public class TestThread implements Runnable{
private int nums;
@Override
public void run() {
nums=10;
while (true) {
System.out.println(Thread.currentThread().getName()+"拿到了第"+nums--+"张票");
if(nums<=0){
break;
}
}
}
public static void main(String[] args) {
TestThread thread = new TestThread();
new Thread(thread,"小明").start();
new Thread(thread,"小红").start();
new Thread(thread,"小华").start();
}
}
运行结果: