线程协助
生产者消费者模式(非设计模式)
方法 | 作用 |
---|
wait() | 表示线程一直等待,直到其他线程通知,与sleep不同,会释放锁 |
wait(long timeout) | 等待指定毫秒数 |
notify() | 唤醒一个正在等待的线程 |
notifyAll() | 唤醒同一个对象上所有调用wait方法的线程,优先级别高的线程先调度 |
- 生产者将生产好的数据放入缓冲区,消费者从缓冲区拿出数据(管程法)
package com.thread.demo;
public class Demo21 {
public static void main(String[] args) {
ChickenContainer chickenContainer = new ChickenContainer();
Consumers consumers = new Consumers(chickenContainer);
Producers producers = new Producers(chickenContainer);
consumers.start();
producers.start();
}
}
class Chicken {
private int id;
public int getId() {
return id;
}
public Chicken(int id) {
this.id = id;
}
}
class Producers extends Thread {
private ChickenContainer container;
public Producers(ChickenContainer container) {
this.container = container;
}
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
Chicken chicken = new Chicken(i);
try {
System.out.println("生产者生产了第"+i+"只鸡");
this.container.addChicken(chicken);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumers extends Thread {
private ChickenContainer container;
public Consumers(ChickenContainer container) {
this.container = container;
}
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
try {
System.out.println("消费者吃了第" + this.container.eatChicken() + "只鸡");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class ChickenContainer {
private Chicken[] chickensContainer = new Chicken[10];
private int count = 0;
public synchronized void addChicken(Chicken chicken) throws InterruptedException {
if (count==chickensContainer.length) {
this.wait();
}
chickensContainer[count] = chicken;
count++;
this.notifyAll();
}
public synchronized int eatChicken() throws InterruptedException {
if (count == 0) {
this.notifyAll();
this.wait();
}
count--;
int id = chickensContainer[count].getId();
return id;
}
}
package com.thread.demo;
public class Demo22 {
public static void main(String[] args) {
Lights lights = new Lights();
new Persons(lights).start();
new Cars(lights).start();
}
}
class Persons extends Thread{
private Lights lights ;
public Persons(Lights lights) {
this.lights = lights;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
boolean ret = this.lights.personRun();
if (ret){
System.out.println("行人通行,汽车等待");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Cars extends Thread{
private Lights lights ;
public Cars(Lights lights) {
this.lights = lights;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
boolean ret = this.lights.carRun();
if (ret){
System.out.println("汽车通行,行人等待");}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Lights{
private boolean flag = true;
public synchronized boolean carRun() throws InterruptedException {
Thread.sleep(100);
if (this.flag){
this.wait();
}
this.notifyAll();
this.flag = !this.flag;
return true;
}
public synchronized boolean personRun() throws InterruptedException {
Thread.sleep(100);
if (!this.flag){
this.wait();
}
this.notifyAll();
this.flag = !this.flag;
return true;
}
}