给我一段 spring boot 多线程抢票 的代码
时间: 2023-03-15 12:03:35 浏览: 154
详解Spring Boot 使用Java代码创建Bean并注册到Spring中
这是一段简单的 spring boot 多线程抢票代码:public class TicketPurchaseThread extends Thread {
private String ticketName; public TicketPurchaseThread(String ticketName) {
this.ticketName = ticketName;
} @Override
public void run() {
try {
Thread.sleep(1000);
System.out.println(ticketName + " 抢票成功!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}public class TicketPurchaseTest {
public static void main(String[] args) {
TicketPurchaseThread ticketPurchaseThread1 = new TicketPurchaseThread("北京-上海");
TicketPurchaseThread ticketPurchaseThread2 = new TicketPurchaseThread("上海-广州");
TicketPurchaseThread ticketPurchaseThread3 = new TicketPurchaseThread("广州-深圳");
ticketPurchaseThread1.start();
ticketPurchaseThread2.start();
ticketPurchaseThread3.start();
}
}
阅读全文