加了 synchronized 为什么还会出现买到负票的情况?
package com.xiaojia.demo06;
public class UpdateSynchronized {
public static void main(String[] args) {
BuyTicket2 station = new BuyTicket2();
new Thread(station,"我").start();
new Thread(station,"你").start();
new Thread(station,"黄牛党").start();
}
}
class BuyTicket2 implements Runnable{
//票
private int ticketNums = 10;
boolean flag = true;//外部停止方式
@Override
public void run() {
//买票
while(flag){
try {
buy();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//synchronized 同步方法,锁的是this,保证线程的安全
private synchronized void buy() throws InterruptedException {
//判断是否有票
if (ticketNums <= 0){
flag = false;
}
//模拟延时
Thread.sleep(200);
//买票
System.out.println(Thread.currentThread().getName()+"买到了第"+ticketNums--+"张票");
}
}
你买到了第10张票
你买到了第9张票
黄牛党买到了第8张票
黄牛党买到了第7张票
我买到了第6张票
我买到了第5张票
黄牛党买到了第4张票
你买到了第3张票
黄牛党买到了第2张票
我买到了第1张票
黄牛党买到了第0张票
你买到了第-1张票
private int ticketNums = 10;加上volatile试试
你这样判断会出现负数啊。你的flag是不可见的,你一个线程在buy里面改了状态,但是其他线程都还没改都卡在循环里呢。所以你需要在flag前加一个volate关键字 使flag发生改变所有线程都可见
买到负的是因为你没有 return。
public class Test{
public static void main(String[] args) {
BuyTicket2 station = new BuyTicket2();
new Thread(station,"我").start();
new Thread(station,"你").start();
new Thread(station,"黄牛党").start();
}
}
class BuyTicket2 implements Runnable{
//票
private int ticketNums = 10;
boolean flag = true;//外部停止方式
@Override
public void run() {
//买票
while(flag){
try {
// 三个人会在这里竞争锁(注意:已经进了 while 循环了)
// 假设当前还有 1 张票,获取锁的顺序假设为:我、你、黄牛
// 当 '我' 卖了 1 号票,'你' 进去就是买的 0 号票, '黄牛' 进去买的就是 -1 号票
// 虽然当 '我' 卖完以后 flag = false 了,但是因为 '你' 和 '黄牛' 已经进了 while,所以能买到票
// 要解决这个问题,就需要加上 return
buy();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//synchronized 同步方法,锁的是this,保证线程的安全
private synchronized void buy() throws InterruptedException {
//判断是否有票
if (ticketNums <= 0){
flag = false;
return;// !!!!!!!!
}
//模拟延时
Thread.sleep(200);
//买票
System.out.println(Thread.currentThread().getName()+"买到了第"+ticketNums--+"张票");
}
}