生产者和消费者模式
管程法
package 多线程.syn;
public class 管程法 {
public static void main(String[] args) {
SynContainer container =new SynContainer();
new Productor(container).start();
new Consumer(container).start();
}
}
//生产者
class Productor extends Thread{
SynContainer synContainer;
public Productor(SynContainer synContainer) {
this.synContainer = synContainer;
}
@Override
public void run() {
//开始生产
for (int i = 1; i < 20; i++) {
System.out.println("生产-->"+i+"个馒头");
synContainer.push(new Steamedbun(i));
}
}
}
//消费者
class Consumer extends Thread{
SynContainer synContainer;
public Consumer(SynContainer synContainer){
this.synContainer=synContainer;
}
@Override
public void run() {
//开始消费
for (int i = 1; i < 20; i++) {
System.out.println("消费了-->"+synContainer.pop().id+"个馒头");
}
}
}
//缓冲区
class SynContainer{
//存储数据的容器
Steamedbun[] buns =new Steamedbun[10];
//计数器
int count=0;
//存储 生产者
public synchronized void push(Steamedbun bun){
//何时能生产 容器存在空间
//不能生产 只有等待
if (count==buns.length){
try {
this.wait();//线程阻塞 消费者通知生产 解除
} catch (InterruptedException e) {
}
}
//存在空间可以生产
buns[count]=bun;
count++;
//存在数据了,可以通知消费了
this.notifyAll();
}
//获取 消费者
public synchronized Steamedbun pop(){
//何时消费,容器是否存在数据
//没有数据只有等待
if(count==0){
try {
this.wait();//线程阻塞 生产者通知消费解除
} catch (InterruptedException e) {
}
}
//存在数据可以消费。从最后一个位置拿
count--;
Steamedbun bun=buns[count];
//消费 有空间了唤醒对方生产
this.notifyAll();
return bun;
}
}
//馒头
class Steamedbun{
int id;
public Steamedbun(int id) {
this.id = id;
}
}
运行结果
生产-->1个馒头
生产-->2个馒头
生产-->3个馒头
生产-->4个馒头
生产-->5个馒头
生产-->6个馒头
生产-->7个馒头
生产-->8个馒头
生产-->9个馒头
生产-->10个馒头
生产-->11个馒头
消费了-->10个馒头
消费了-->11个馒头
消费了-->9个馒头
消费了-->8个馒头
消费了-->7个馒头
消费了-->6个馒头
消费了-->5个馒头
消费了-->4个馒头
消费了-->3个馒头
消费了-->2个馒头
消费了-->1个馒头
生产-->12个馒头
生产-->13个馒头
消费了-->12个馒头
消费了-->13个馒头
生产-->14个馒头
生产-->15个馒头
消费了-->14个馒头
消费了-->15个馒头
生产-->16个馒头
生产-->17个馒头
生产-->18个馒头
消费了-->16个馒头
消费了-->18个馒头
消费了-->17个馒头
生产-->19个馒头
消费了-->19个馒头
Process finished with exit code 0
信号灯法 标志位
package 多线程.syn;
public class 信号灯法 {
public static void main(String[] args) {
Tv tv =new Tv();
new Player(tv).start();
new Watcher(tv).start();
}
}
//生产者->演员
class Player extends Thread{
Tv tv;
public Player(Tv tv) {
this.tv = tv;
}
@Override
public void run() {
for (int i = 1; i <=20; i++) {
if(i%2==0){
this.tv.play("<快乐大本营播放中>"+i);
}else{
this.tv.play("<抖音记录美好生活>"+i);
}
}
}
}
//消费者->观众
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{
//演员表演,观众等待 T
//观众观看,演员等待 F
String voice;//表演的节目
boolean flag =true;
//表演
public synchronized void play(String voice){
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("演员表演了:"+voice);
//通知观众观看
this.notifyAll();//通知唤醒
this.voice=voice;
this.flag=!this.flag;
}
//观看
public synchronized void watch(){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
}
}
System.out.println("观看了:"+voice);
//通知演员表演
this.notifyAll();
this.flag=!this.flag;
}
}
运行结果
演员表演了:<抖音记录美好生活>1
观看了:<抖音记录美好生活>1
演员表演了:<快乐大本营播放中>2
观看了:<快乐大本营播放中>2
演员表演了:<抖音记录美好生活>3
观看了:<抖音记录美好生活>3
演员表演了:<快乐大本营播放中>4
观看了:<快乐大本营播放中>4
演员表演了:<抖音记录美好生活>5
观看了:<抖音记录美好生活>5
演员表演了:<快乐大本营播放中>6
观看了:<快乐大本营播放中>6
演员表演了:<抖音记录美好生活>7
观看了:<抖音记录美好生活>7
演员表演了:<快乐大本营播放中>8
观看了:<快乐大本营播放中>8
演员表演了:<抖音记录美好生活>9
观看了:<抖音记录美好生活>9
演员表演了:<快乐大本营播放中>10
观看了:<快乐大本营播放中>10
演员表演了:<抖音记录美好生活>11
观看了:<抖音记录美好生活>11
演员表演了:<快乐大本营播放中>12
观看了:<快乐大本营播放中>12
演员表演了:<抖音记录美好生活>13
观看了:<抖音记录美好生活>13
演员表演了:<快乐大本营播放中>14
观看了:<快乐大本营播放中>14
演员表演了:<抖音记录美好生活>15
观看了:<抖音记录美好生活>15
演员表演了:<快乐大本营播放中>16
观看了:<快乐大本营播放中>16
演员表演了:<抖音记录美好生活>17
观看了:<抖音记录美好生活>17
演员表演了:<快乐大本营播放中>18
观看了:<快乐大本营播放中>18
演员表演了:<抖音记录美好生活>19
观看了:<抖音记录美好生活>19
演员表演了:<快乐大本营播放中>20
观看了:<快乐大本营播放中>20
Process finished with exit code 0
Thread 和 Runnable 的区别
Thread和Runnable的实质是继承关系,没有可比性。无论使用Runnable还是Thread, 都会new Thread,然后执行run方法。用法上,如果有复杂的线程操作需求,那就选择继承Thread, 如果只是简单的执行一个任务,那就实现runnable。