线程并发及线程并发问题的解决
程序员文章站
2022-05-04 20:59:42
...
火车站多个窗口同时售票,假设总共500张票,四个窗口同时卖。
1.创建一个继承Thread类的子类
package com.commons.commons.taskthread;
/**
* @Author zhaomengxia
* @create 2019/10/12 16:56
*/
public class SaleTicketThread extends Thread {
static int number = 500;
private String name;
public SaleTicketThread(String name) {
this.name = name;
}
@Override
public void run() {
while (true) {
if (number > 0) {
System.out.println(name + "卖出" + (number--) + "号");
} else {
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name + "票卖完了!");
}
}
测试 类
package com.commons.commons.taskthread;
/**
* @Author zhaomengxia
* @create 2019/10/12 17:02
*/
public class SaleTicketThreadTest {
public static void main(String[] args) {
SaleTicketThread saleTicketThread=new SaleTicketThread("窗口1");
SaleTicketThread saleTicketThread1=new SaleTicketThread("窗口2");
SaleTicketThread saleTicketThread2=new SaleTicketThread("窗口3");
SaleTicketThread saleTicketThread3=new SaleTicketThread("窗口4");
saleTicketThread.start();
saleTicketThread1.start();
saleTicketThread2.start();
saleTicketThread3.start();
}
}
从运行结果中我们可以看到有出现不同窗口卖同一张票的现象
接下来就引出
修改后的代码如下,就解决了上诉出现的并发问题,不会出现不同窗口卖出同一张票的情况。
package com.commons.commons.taskthread;
/**
* @Author zhaomengxia
* @create 2019/10/12 16:56
*/
public class SaleTicketThread extends Thread {
static int number = 500;
private String name;
//创建一个锁对象,这个对象是多个线程对象共享的数据
static Object object=new Object();
public SaleTicketThread(String name) {
this.name = name;
}
@Override
public void run() {
while (true) {
synchronized (object){
if (number > 0) {
System.out.println(name + "卖出" + (number--) + "号");
} else {
break;
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name + "票卖完了!");
}
}