文章目录
卖票问题
问题描述
如下代码是实现四个窗口同时卖票的场景,但是从输出结果可以看出,在实现数据共享的同时,数据安全无法保证。
方式1: 继承Thread类
public class TicketThread extends Thread{
private static int ticket = 6;
@Override
public void run() {
for(int i = 0;i < 10; i++){
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() + "正在出售第" + (ticket--) + "张票");
}
}
}
public static void main(String[] args) {
TicketThread t1 = new TicketThread();
TicketThread t2 = new TicketThread();
TicketThread t3 = new TicketThread();
TicketThread t4 = new TicketThread();
t1.start();
t2.start();
t3.start();
t4.start();
}
}
方式2: 实现runnable接口
public class TicketRunnable implements Runnable {
private int ticket = 5;
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() + "正在出售第" + (ticket--) + "张票");
}
}
}
public static void main(String[] args) {
TicketRunnable ticket = new TicketRunnable();
Thread t1 = new Thread(ticket);
Thread t2 = new Thread(ticket);
Thread t3 = new Thread(ticket);
Thread t4 = new Thread(ticket);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
运行结果
Thread-2正在出售第6张票
Thread-0正在出售第4张票
Thread-3正在出售第3张票
Thread-1正在出售第5张票
Thread-0正在出售第1张票
Thread-2正在出售第2张票
问题解决
使用线程同步,改进后代码:
方式1: 使用同步代码块
public class TicketRunnable2 implements Runnable {
private int ticket = 5;
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (this){
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() + "正在出售第" + (ticket--) + "张票");
}
}
}
}
public static void main(String[] args) {
TicketRunnable2 ticket = new TicketRunnable2();
Thread t1 = new Thread(ticket,"A");
Thread t2 = new Thread(ticket,"B");
Thread t3 = new Thread(ticket,"C");
Thread t4 = new Thread(ticket,"D");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
方式2: 使用同步方法
public class TicketRunnable3 implements Runnable {
private int ticket = 5;
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.sale();
}
}
/*
* 使用同步方法解决多线程数据安全的问题
* */
public synchronized void sale() {
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() + "正在出售第" + (ticket--) + "张票");
}
}
public static void main(String[] args) {
TicketRunnable3 ticket = new TicketRunnable3();
Thread t1 = new Thread(ticket, "A");
Thread t2 = new Thread(ticket, "B");
Thread t3 = new Thread(ticket, "C");
Thread t4 = new Thread(ticket, "D");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
运行结果
A正在出售第5张票
C正在出售第4张票
D正在出售第3张票
B正在出售第2张票
D正在出售第1张票
生产者消费者问题
问题描述
下面代码模拟生产者生产产品,消费者取走商品的过程,首先创建生产者、消费者和产品三个类。从实现结果可以看出:
多线程访问的时候出现了数据安全的问题
1、生产者没有生产商品,消费者就可以获取
2、商品的品牌和名称对应不上
产品
public class Goods {
private String brand;
private String name;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
生产者
/*
*
* 生产产品,将产品放置到共享空间中
*
* */
public class Producer implements Runnable {
private Goods goods;
public Producer(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
goods.setBrand("娃哈哈");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
goods.setName("矿泉水");
} else {
goods.setBrand("旺仔");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
goods.setName("小馒头");
}
System.out.println("生产者生产了" + this.goods.getBrand() + "--" + this.goods.getName());
}
}
}
消费者
/*
*
* 从共享空间中取走产品
* */
public class Consumer implements Runnable {
private Goods goods;
public Consumer(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
for(int i = 0;i<10;i++){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("消费者取走了"+this.goods.getBrand()+"----"+this.goods.getName());
}
}
}
Test类
public class Test {
public static void main(String[] args) {
Goods goods = new Goods();
Producer producer = new Producer(goods);
Consumer consumer = new Consumer(goods);
Thread t1 = new Thread(producer);
Thread t2 = new Thread(consumer);
t1.start();
t2.start();
}
}
运行结果
消费者取走了娃哈哈----矿泉水
生产者生产了娃哈哈--矿泉水
消费者取走了旺仔----小馒头
生产者生产了旺仔--小馒头
生产者生产了娃哈哈--矿泉水
消费者取走了娃哈哈----矿泉水
消费者取走了旺仔----矿泉水
生产者生产了旺仔--小馒头
生产者生产了娃哈哈--矿泉水
消费者取走了娃哈哈----矿泉水
消费者取走了旺仔----小馒头
生产者生产了旺仔--小馒头
生产者生产了娃哈哈--矿泉水
消费者取走了娃哈哈----小馒头
消费者取走了旺仔----小馒头
生产者生产了旺仔--小馒头
消费者取走了娃哈哈----矿泉水
生产者生产了娃哈哈--矿泉水
消费者取走了旺仔----小馒头
生产者生产了旺仔--小馒头
问题解决
1)首先解决商品的品牌和名称对应不上问题,对象使用同步方法
产品
public class Goods {
private String brand;
private String name;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//消费者获取商品
public synchronized void get(){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("消费者取走了"+this.getBrand()+"----"+this.getName());
}
//生产者生产商品
public synchronized void set(String brand,String name){
this.setBrand(brand);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.setName(name);
System.out.println("生产者生产了" + this.getBrand() + "--" + this.getName());
}
}
生产者
public class Producer implements Runnable {
private Goods goods;
public Producer(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
goods.set("娃哈哈","矿泉水");
} else {
goods.set("旺仔","小馒头");
}
}
}
}
消费者
public class Consumer implements Runnable {
private Goods goods;
public Consumer(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
for(int i = 0;i<10;i++){
goods.get();
}
}
}
Test类
public class Test {
public static void main(String[] args) {
Goods goods = new Goods();
Producer producer = new Producer(goods);
Consumer consumer = new Consumer(goods);
Thread t1 = new Thread(producer);
Thread t2 = new Thread(consumer);
t1.start();
t2.start();
}
}
2)解决生产者没有生产商品,消费者就可以获取的问题
添加一个布尔类型标志flag,默认是false代表没有商品,true表示有商品
产品类
public class Goods {
private String brand;
private String name;
//默认是不存在商品的,如果值等于true的话,代表有商品
private boolean flag = false;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//消费者获取商品
public synchronized void get(){
/*
* 如果flag等于false的话,意味着生产者没有生产商品,此时消费者无法消费,需要让消费者线程进入到阻塞状态,等待生产者生产,当
* 有商品之后,再开始消费
* */
if (!flag){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("消费者取走了"+this.getBrand()+"----"+this.getName());
flag = false;
//唤醒生产者去进行生产
notify();
}
//生产者生产商品
public synchronized void set(String brand,String name){
//当生产者抢占到cpu资源之后会判断当前对象是否有值,如果有的话,以为着消费者还没有消费,需要提醒消费者消费,同时
//当前线程进入阻塞状态,等待消费者取走商品之后,再次生产,如果没有的话,不需要等待,不需要进入阻塞状态,直接生产即可
if(flag){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.setBrand(brand);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.setName(name);
System.out.println("生产者生产了" + this.getBrand() + "--" + this.getName());
//如果代码执行到此处,意味着已经生产完成,需要将flag设置为true
flag = true;
//唤醒消费者去进行消费
notify();
}
}
生产者
public class Producer implements Runnable {
private Goods goods;
public Producer(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
goods.set("娃哈哈","矿泉水");
} else {
goods.set("旺仔","小馒头");
}
}
}
}
消费者
public class Consumer implements Runnable {
private Goods goods;
public Consumer(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
for(int i = 0;i<10;i++){
goods.get();
}
}
}
Test类
public class Test {
public static void main(String[] args) {
Goods goods = new Goods();
Producer producer = new Producer(goods);
Consumer consumer = new Consumer(goods);
Thread t1 = new Thread(producer);
Thread t2 = new Thread(consumer);
t1.start();
t2.start();
}
}
3)用JUC中的BlockingQueue类实现
产品类
public class Goods {
private String brand;
private String name;
public Goods(String brand, String name) {
this.brand = brand;
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
生产者
public class ProducerQueue implements Runnable {
private BlockingQueue<Goods> blockingQueue;
public ProducerQueue(BlockingQueue blockingQueue) {
this.blockingQueue = blockingQueue;
}
@Override
public void run() {
for(int i = 0;i<10;i++){
Goods goods = null;
if(i%2==0){
goods = new Goods("娃哈哈","矿泉水");
}else{
goods = new Goods("旺仔","小馒头");
}
System.out.println("生产者开始生产商品:"+goods.getBrand()+"--"+goods.getName());
try {
blockingQueue.put(goods);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
消费者
public class ConsumerQueue implements Runnable {
private BlockingQueue<Goods> blockingQueue;
public ConsumerQueue(BlockingQueue blockingQueue) {
this.blockingQueue = blockingQueue;
}
@Override
public void run() {
for(int i = 0;i<10;i++){
try {
Goods goods = blockingQueue.take();
System.out.println("消费者消费的商品是:"+goods.getBrand()+"--"+goods.getName());
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Test类
public class Test {
public static void main(String[] args) {
BlockingQueue<Goods> queue = new ArrayBlockingQueue<Goods>(5);
ProducerQueue producerQueue = new ProducerQueue(queue);
ConsumerQueue consumerQueue = new ConsumerQueue(queue);
new Thread(producerQueue).start();
new Thread(consumerQueue).start();
}
}