JAVA執行緒模擬火車站賣票
阿新 • • 發佈:2019-01-04
package com.nisec.myapplication; public class test { static int tickets = 10; public static void main(String[] args) { for(int i=1;i<=5;i++){ Thread t = new Thread(new Runnable() { @Override public void run(){ while(tickets>0){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } if(tickets<=0){ break; } synchronized (test.class){ System.out.println(Thread.currentThread().getName()+"賣出第"+(tickets--)+"張票"); } } } },"第"+i+"售票點"); t.start(); } } }