1. 程式人生 > 其它 >uniapp專案h5端使用vant元件

uniapp專案h5端使用vant元件

技術標籤:java多執行緒

萬人如海,一身藏。

4個視窗同時賣10張票,賣票間隔20秒
輸出每個視窗實時賣票情況

在這裡插入圖片描述

public class Ticket extends Thread{
	public Ticket() {
		 super();
		 }
		 private static Object obj = new Object();
		 public static int tickets = 10;
		 
		 public void run() {
		 while(tickets>0) {
		 synchronized (obj) {
		 if(tickets >
0) { try { sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } tickets--; System.out.println(currentThread().getName()+"賣了第"+(10-tickets)+"張票"); } } } } public static void main(String[] args) {
Ticket ticket=new Ticket(); new Thread(ticket,"視窗1").start(); new Thread(ticket,"視窗2").start(); new Thread(ticket,"視窗3").start(); new Thread(ticket,"視窗4").start(); } }