西宁做网站君博领先,网站开发中期检查,php学生管理系统源码免费,在什么网站做推广MultiProcessingExercise
package MultiProcessingExercise120240813;public class MultiProcessingExercise {public static void main(String[] args) {/*需求#xff1a;一共有1000张电影票,可以在两个窗口领取,假设每次领取的时间为3000毫秒,请用多线程模拟卖票过程并打印… MultiProcessingExercise
package MultiProcessingExercise120240813;public class MultiProcessingExercise {public static void main(String[] args) {/*需求一共有1000张电影票,可以在两个窗口领取,假设每次领取的时间为3000毫秒,请用多线程模拟卖票过程并打印剩余电影票的数量*/SellTicketsPro sellTicketsPro new SellTicketsPro();Thread thread1 new Thread(sellTicketsPro, 窗口1);Thread thread2 new Thread(sellTicketsPro, 窗口2);thread1.start();thread2.start();}
}SellTickets
package MultiProcessingExercise120240813;public class SellTickets implements Runnable {private static int tickets 100;private static final Object lock new Object();Overridepublic void run() {while (true) {synchronized(lock) {if (tickets 0) {break;} else {try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}tickets--;System.out.println(Thread.currentThread().getName() 正在卖第 (100 - tickets) 张票);}}}}
}SellTicketsPro
package MultiProcessingExercise120240813;import java.util.concurrent.locks.ReentrantLock;public class SellTicketsPro implements Runnable {private static int tickets 1000;private static final ReentrantLock lock new ReentrantLock();Overridepublic void run() {while (tickets 0) {lock.lock();try {if (tickets 0) {try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}tickets--;System.out.println(Thread.currentThread().getName() 正在卖第 (1000 - tickets) 张票);}} finally {lock.unlock();}}}
}