1、线程通信相关API
- wait():表示线程一直等待,直到其它线程通知,与sleep不同,会释放锁
- wait(long timeout):指定等待的毫秒数
- notify():唤醒一个处于等待状态的线程
- notifyAll():唤醒同一个对象上所有调用wait()方法的线程,优先级别高的线程优先调度
注意:以上均是Object 类的方法,都只能在同步方法和同步代码块中使用,否则抛出异常lllegalMonitoStateException
2、生产者消费者模型:管程法
/**
* 生产者消费者模型-->利用缓冲区解决:管程法
*/
public class TestPC {
public static void main(String[] args) {
SynContainer container = new SynContainer();
new Productor(container).start();
new Consummer(container).start();
}
}
// 生产者
class Productor extends Thread{
SynContainer synContainer;
public Productor(SynContainer synContainer){
this.synContainer = synContainer;
}
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synContainer.push(new Chicken(i));
System.out.println("生产了" + i + "只鸡");
}
}
}
// 消费者
class Consummer extends Thread{
SynContainer synContainer;
public Consummer(SynContainer synContainer){
this.synContainer = synContainer;
}
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
Chicken chicken = synContainer.pop();
System.out.println("消费了--》" + chicken.id +"只鸡");
}
}
}
// 产品
class Chicken{
int id;
public Chicken(int id) {
this.id = id;
}
}
// 缓冲区
class SynContainer{
// 容器大小
Chicken[] chickens = new Chicken[10];
// 定义一个计数器
int count = 0;
// 生产者放入产品
public synchronized void push(Chicken chicken) {
// 如果容器满了,需要等待消费者消费
if (count == chickens.length){
// 通知消费消费,生产者等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 如果没有满就丢入鸡
chickens[count] = chicken;
count++;
//可通知消费者消费了
this.notifyAll();
}
// 消费者消费产品
public synchronized Chicken pop() {
if (count ==0){
// 等待生产者生产
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 如果有鸡就消费
count--;
Chicken chicken = chickens[count];
// 吃完了通知生产者生产
this.notifyAll();
return chicken;
}
}
3、生产者消费者模型:信号灯法
package com.study.thread;
/**
* 生产者消费者模型-->信号灯法:标志位解决
*/
public class TestPC {
public static void main(String[] args) {
TV tv = new TV();
Singer singer = new Singer(tv);
Watcher watcher = new Watcher(tv);
singer.start();
watcher.start();
}
}
// 生产者: 歌手
class Singer extends Thread{
TV tv;
public Singer(TV tv){
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
tv.play("爱你一万年");
}
}
}
// 消费者: 观众
class Watcher extends Thread{
TV tv;
public Watcher(TV tv){
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
tv.watch();
}
}
}
// 产品: 节目
class TV{
String song;
// 歌手表演,观众等待 true
// 观众观看,歌手等待 false
boolean flag = true;
// 表演
public synchronized void play(String song){
if (!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("歌手唱了一首歌,名字为"+ song);
this.notifyAll();
this.song = song;
this.flag = !this.flag;
}
// 观看
public synchronized void watch(){
if (flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("观众听了歌曲" + song);
// 通知演员表演
this.notifyAll();
this.flag = !this.flag;
}
}