您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么用java多線程模擬實現售票功能”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么用java多線程模擬實現售票功能”吧!
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(); } } //鎖對象:類的字節碼文件對象(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 任務類
測試方法:
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();//手動關鎖 } } }
效果截圖:
感謝各位的閱讀,以上就是“怎么用java多線程模擬實現售票功能”的內容了,經過本文的學習后,相信大家對怎么用java多線程模擬實現售票功能這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。