生产者与消费者线程并发经典问题
方式一:
private static List<Productss> productsses = new ArrayList<>();
private static Random random = new Random();
public static void main(String[] args) {
// 生产者
new Thread(() -> {
synchronized (productsses) {
while (true) {
while (productsses.size() >= 5) {
try {
System.out.println("生产者在等待...");
productsses.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
Productss p = new Productss();
productsses.add(p);
System.out.println(Thread.currentThread().getName() + "生产者生产了" + p);
productsses.notifyAll();
}
}
}).start();
for (int i = 0; i < 10; i++) {
// 多个消费者并发
new Thread(() -> {
synchronized (productsses) {
while (true) {
while (productsses.size() == 0) {
try {
System.out.println(Thread.currentThread().getName() + "在等待...");
productsses.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(random.nextInt(500));
} catch (InterruptedException e) {
e.printStackTrace();
}
Productss p = productsses.remove(0);
System.out.println(Thread.currentThread().getName() + "消费者消费了" + p);
productsses.notifyAll();
}
}
}).start();
}
}
方式二:
package org.westos.thread.consumer;
import java.util.Random;
/**
* *************************************
* Copyright (c) 2018 feiyan.com
* All rights reserved.
* *************************************
* *************************************
*
* @Author: think
* @Project: JavaSE
* @Date: Created in 2018/8/12 16:37
* @Since: JDK 1.8
* @Version: 1.0
* @Modified By:
* @Description:
*/
public class ConsumerAndProduct {
// 标记是否生产产品 true : 是 false : 否
public static boolean flag = false;
public static final Object OBJ = new Object();
private static Product product = null;
static class Producter extends Thread {
@Override
public void run() {
synchronized (OBJ) {
while (true) {
if (!flag) {
Fruit[] fruits = Fruit.values();
Random random = new Random();
product = new Product(fruits[random.nextInt(4)].toString(),
3.5*random.nextInt(4));
flag = true;
System.out.println("生产者生产了--" + product);
OBJ.notify();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
try {
OBJ.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
static class Consumer extends Thread {
@Override
public void run() {
synchronized (OBJ) {
while (true) {
if (!flag) {
try {
OBJ.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System.out.println("消费者消费了--" + product);
flag = false;
OBJ.notify();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
public static void main(String[] args) {
Producter producter = new Producter();
Consumer consumer = new Consumer();
producter.start();
consumer.start();
}
}
enum Fruit{
Apple, Banana, Peak, Grape
}
class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public String toString() {
return name + " : " + price;
}
}
线程并发卖票经典问题
package org.westos.thread;
/**
* *************************************
* Copyright (c) 2018 feiyan.com
* All rights reserved.
* *************************************
* *************************************
*
* @Author: think
* @Project: JavaSE
* @Date: Created in 2018/8/12 11:38
* @Since: JDK 1.8
* @Version: 1.0
* @Modified By:
* @Description:${description}
*/
class SaleStick extends Thread {
public static int stick = 50;
public static final Object OBJ = new Object();
public SaleStick(String name){
super(name);
}
@Override
public void run() {
synchronized (OBJ) {
while (true) {
if (stick > 0) {
System.out.println(Thread.currentThread().getName() + "卖出了"
+ stick + "号票.");
stick--;
try {
OBJ.wait(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System.out.println("卖完了。。。");
break;
}
}
}
}
}
public class ThreadSecurityTest {
public static void main(String[] args) {
SaleStick t1 = new SaleStick("1号窗口");
SaleStick t2 = new SaleStick("2号窗口");
SaleStick t3 = new SaleStick("3号窗口");
t1.start();
t2.start();
t3.start();
}
}