写一个多线程卖票的程序
/** * @author tom * @date 2022-03-29 15:33 */ public class SaleTickets { public static void main(String[] args) { T t = new T(); for (int i = 0; i < 10; i++) { new Thread(t).start(); } } } class T implements Runnable{ // 定义售票数量 private static int tickets = 100; @Override public void run() { while (true){ // 锁住当前对象 synchronized (this){ if (tickets >= 1){ System.out.println(Thread.currentThread().getName() + " 正在售卖第 " + tickets + " 张票 "); tickets--; } else { System.out.println(Thread.currentThread().getName() + " 票已经卖完"); break; } } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }