您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么用java多線程模擬實現(xiàn)售票功能”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么用java多線程模擬實現(xiàn)售票功能”吧!
1 線程類
測試方法:
public static void main(String[] args) { MyThread t1 = new MyThread("窗口1"); MyThread t2 = new MyThread("窗口1"); MyThread t3 = new MyThread("窗口1"); t1.start(); t2.start(); t3.start(); }
1.1 局部加鎖
public class MyThread extends Thread{ private static int ticket = 1000; private static Object obj = new Object(); public MyThread(String name) { super(name); } @Override public void run() { while(ticket > 0){ synchronized(obj){ if(ticket > 0){ System.out.println(Thread.currentThread().getName() + "正在銷售第" +(1001-ticket)+ "張票"); ticket--; } if(ticket <= 0){ System.out.println(Thread.currentThread().getName() + "票已售罄"); } } } } }
1.2 方法加鎖
public class MyThread extends Thread{ private static int ticket = 1000; public MyThread(String name) { super(name); } @Override public void run() { while(ticket > 0){ method02(); } } //鎖對象:類的字節(jié)碼文件對象(MyThread.class),有static修飾 public static synchronized void method02(){ if(ticket > 0){ System.out.println(Thread.currentThread().getName() + "正在銷售第" +(1001-ticket)+ "張票"); ticket--; } if(ticket <= 0){ System.out.println(Thread.currentThread().getName() + "票已售完"); } } }
1.3 手動加鎖
public class MyThread extends Thread{ private static int ticket = 1000; private static Lock lock = new ReentrantLock(); public MyThread(String name) { super(name); } @Override public void run() { while(ticket > 0){ lock.lock();//手動上鎖 if(ticket > 0){ System.out.println(Thread.currentThread().getName() + "正在銷售第" +(1001-ticket)+ "張票"); ticket--; } if(ticket <= 0){ System.out.println(Thread.currentThread().getName() + "票已售完"); } lock.unlock();//手動解鎖 } } }
2 任務(wù)類
測試方法:
public static void main(String[] args) { Task task = new Task(); Thread t1 = new Thread(task, "窗口1"); Thread t2 = new Thread(task, "窗口2"); Thread t3 = new Thread(task, "窗口3"); t1.start(); t2.start(); t3.start(); }
2.1 局部加鎖
public class Task implements Runnable{ private int tickets=1000; @Override public void run() { while(tickets>0){ synchronized (this) { if(tickets > 0) { System.out.printf ("%s窗口正在售出第%d張票\n",Thread.currentThread().getName(),1001-tickets); tickets--; } if(tickets<=0){ System.out.printf("%s窗口售罄\n",Thread.currentThread().getName()); } } } } }
2.2 方法加鎖
public class Task implements Runnable{ private int tickets=1000; @Override public void run() { while(tickets>0){ method(); } } //方法加鎖,沒有使用static修飾 public synchronized void method(){ if(tickets > 0) { System.out.printf ("%s窗口正在售出第%d張票\n",Thread.currentThread().getName(),1001-tickets); tickets--; } if(tickets<=0){ System.out.printf("%s窗口售罄\n",Thread.currentThread().getName()); } } }
2.3 手動加鎖
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class Task implements Runnable{ private int tickets=1000; private Lock lock =new ReentrantLock(); @Override public void run() { while(tickets>0){ lock.lock();//手動上鎖 if(tickets > 0) { System.out.printf ("%s窗口正在售出第%d張票\n",Thread.currentThread().getName(),1001-tickets); tickets--; } if(tickets<=0){ System.out.printf("%s窗口售罄\n",Thread.currentThread().getName()); } lock.unlock();//手動關(guān)鎖 } } }
效果截圖:
感謝各位的閱讀,以上就是“怎么用java多線程模擬實現(xiàn)售票功能”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對怎么用java多線程模擬實現(xiàn)售票功能這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。