最近刚开始学java的Synchronized关键字,了解到互斥锁,测试了一下,但是结果却跟学到的不一样,我模拟的是卖票的情况,退出条件为卖完即退出,大多数情况下都能正常,但是多测几次发现会出现超卖的情况,谁能帮忙解答一下吗?
package concurrent;
public class SellTicketTest {
public static void main(String[] args) {
SellTicket sellTicket = new SellTicket();
Thread thread = new Thread(sellTicket);
Thread thread1 = new Thread(sellTicket);
Thread thread2 = new Thread(sellTicket);
Thread thread3 = new Thread(sellTicket);
thread3.start();
thread.start();
thread1.start();
thread2.start();
}
}
class SellTicket implements Runnable{
private int ticketNum=100;
private boolean loop=true;
public synchronized void sell(){//加锁的位置
System.out.println(Thread.currentThread().getName() + "窗口卖出一张票" + "还剩下" + (--ticketNum) + "张票");
if(ticketNum<=0){//退出的条件
System.out.println("售票结束");
loop=false;
}
}
@Override
public void run() {
while (loop){
sell();
}
}
}
Thread-2窗口卖出一张票还剩下1张票
Thread-2窗口卖出一张票还剩下0张票
售票结束
Thread-1窗口卖出一张票还剩下-1张票
售票结束
Thread-0窗口卖出一张票还剩下-2张票
售票结束
Thread-3窗口卖出一张票还剩下-3张票
售票结束
本以为是加锁的位置不对,或者是创建了不同的对象,但是检查后发现并没有创建多个对象
有哪位能指点迷津吗?感谢感谢
进方法后要判断下票数。
public synchronized void sell(){//加锁的位置
if (ticketNum > 0) {
System.out.println(Thread.currentThread().getName() + "窗口卖出一张票" + "还剩下" + (--ticketNum) + "张票");
} else {
System.out.println("售票结束");
loop=false;
}
}
你这个加的是对象锁,你启了4个线程,4给线程对于的锁对象为4个,互不影响的所以加锁无效果。
https://www.cnblogs.com/codebj/p/10994748.html
public synchronized void sell()
改为
public static synchronized void sell()
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!