資源簡介
多線程同步解決賣票問題
代碼片段和文件信息
class?SellThread?implements?Runnable{
int?tickets?=?100;
public?synchronized?void?sell(){
if(tickets?>?0){
/*
try{
Thread.sleep(10);
}catch(Exception?e){
e.printStackTrace();
}
*/
System.out.println(Thread.currentThread().getName()+“?has?selled?tickets:“+tickets);
tickets--;
評論
共有 條評論