java<\/a>.util.concurrent.locks.Lock;
public class ticket implements Runnable{
private int ticketnum = 100; \/\/\u5168\u5c40\u53d8\u91cf
Lock lock = new ReentrantLock(true); \/\/\u5bf9\u8c61
@Override
public void run() { \/\/\u5199 run\uff08\uff09
while (ticketnum > 0){
lock.lock(); \/\/\u52a0\u9501
try {
if (ticketnum > 0){
try {
Thread.sleep(100); \/\/\u5de5\u4f5c\u65f6\u95f4
} catch (InterruptedException e) {
e.printStackTrace();
}
String name = new Thread().currentThread().getName();
System.out.println("\u7ebf\u7a0b"+name+"\u9500\u552e\u7535\u5f71\u7968"+ticketnum--);
}
}
finally {
lock.unlock(); \/\/\u89e3\u9501
}
}
}
}
\u5c06\u65b9\u6cd5\u52a0\u5165\u5230\u7ebf\u7a0b\u4e2d<\/pre> \n package com.company;
import com.company.ticket;
public class shoupiao {
public static void main(String[] args) {
ticket ticket = new ticket();
Thread thread1 = new Thread(ticket,"\u7a97\u53e31");
Thread thread2 = new Thread(ticket,"\u7a97\u53e32");
Thread thread3 = new Thread(ticket,"\u7a97\u53e33");
thread1.start();
thread2.start();
thread3.start();
}
}<\/pre> \n
<\/pre>\n<\/div>","orderid":"0","title":"java\u7ebf\u7a0b\u9501\u57fa\u7840","smalltitle":"","mid":"0","fname":"Erlang","special_id":"0","bak_id":"0","info":"0","hits":"317","pages":"1","comments":"0","posttime":"2019-08-15 23:30:43","list":"1565883043","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"java<\/A> \u7ebf\u7a0b<\/A> \u57fa\u7840<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"120.229.33.178","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"java\u7ebf\u7a0b\u9501\u57fa\u7840","lastview":"1714180861","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}