不安全线程案例1
//不安全的買票public class UnsafeBuyTicket {public static void main(String[] args) {BuyTicket station = new BuyTicket();new Thread(station,"我").start();new Thread(station,"你").start();new Thread(station,"他").start();}}class BuyTicket implements Runnable{//票private int ticketNums = 10;boolean flag = true;//外部停止方式@Overridepublic void run() {//買票while (flag){try {buy();} catch (InterruptedException e) {e.printStackTrace();}}}private void buy() throws InterruptedException {//判斷是否有票if (ticketNums <= 0){return;}//模擬延時Thread.sleep(100);//買票System.out.println(Thread.currentThread().getName()+"拿到"+ticketNums--);}
}
總結
- 上一篇: 老男孩python培训价格
- 下一篇: 敏捷方法 - 精益思想