代码拉取完成,页面将自动刷新
package com.yolovf.knowledge.test.thread.test;
/**
* @Author: xbcao
* @Description: TODO
* @Date: 2023/11/29 17:52:11
* @Since: jdk1.8
* @Version: 1.0
*/
public class SellTicketsTestTwo {
public static void main(String[] args) {
ThreadRunnableTwo threadRunnable = new ThreadRunnableTwo();
Thread t1 = new Thread(threadRunnable);
t1.setName("pdd");
Thread t2 = new Thread(threadRunnable);
t2.setName("jd");
Thread t3 = new Thread(threadRunnable);
t3.setName("tb");
t1.start();
t2.start();
t3.start();
}
}
class ThreadRunnableTwo implements Runnable{
private int ticketsNum = 0;
private int pddTickets = 0; //pdd最多购买4张
@Override
public void run() {
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
synchronized (ThreadRunnableTwo.class) {
if (ticketsNum == 30) {
break;
}else {
if ("pdd".equals(Thread.currentThread().getName())) {
if (pddTickets >= 4) {
System.out.println("pdd已卖完");
break;
} else {
pddTickets++;
System.out.println(Thread.currentThread().getName() + "--->" + (++ticketsNum) + "张票");
}
} else {
System.out.println(Thread.currentThread().getName() + "正在出售第" + (++ticketsNum) + "张票");
}
}
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。