生产者-消费者问题
这个问题其实非常简单,就是生产者生产出东西,放到缓冲区,消费者消费,东西没了消费者通知生产者生产,生产者生产完了通知消费者消费。
注意:它和静态代理模式不同,它只是一个问题,不是设计模式之一蛤~
我们写两个例子说明这个问题:
管程法
package Thread;
//测试生产者-消费者模型-->利用缓冲区解决:管程法
// 生产者,消费者,产品,缓冲区
public class TestPC {
public static void main(String[] args) {
SynContainer container = new SynContainer();
Productor productor = new Productor(container);
Consumer consumer = new Consumer(container);
productor.start();
consumer.start();
}
}
// 生产者
class Productor extends Thread{
SynContainer container;
public Productor(SynContainer container){
this.container = container;
}
// 生产
@Override
public void run() {
for (int i = 0; i < 100; i++) {
container.push(new Chicken(i));
System.out.println("生产了"+i+"只鸡");
}
}
}
// 消费者
class Consumer extends Thread{
SynContainer container;
public Consumer(SynContainer container){
this.container = container;
}
//消费
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("消费了-->"+container.pop().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==10)
{
// 通知消费者消费,生产者等待
try {
this.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
//如果没有满,我们就需要丢入产品
chickens[count] = chicken;
count++;
// 可以通知消费者消费了
this.notifyAll();
}
// 消费者消费产品
public synchronized Chicken pop()
{
// 判断能否消费
if(count==0)
{
// 等待生产者生产,消费者等待
try {
this.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
// 如果可以消费
count--;
Chicken chicken = chickens[count];
//通知生产者生产
this.notifyAll();
return chicken;
}
}
首要的两个类是生产者和消费者,它们都继承了Thread类,然后就是产品类,最后是缓冲区类,在生产者和消费者类中,重写run方法时,他们其实就只是负责生产和消费而已,我在代码中的表现就是调用一下缓冲区的方法,然后输出一下生产/消费了第几个产品;对于产品,直接给它构造一些属性即可;重点是缓冲区类的书写,首先需要一个容器(数组),我这里定义了大小为10的容器,然后分别写了生产者生产的方法以及消费者消费的方法(注意this.wait 以及 this.notifyAll 方法,这是为了通知等待以及通知消费/生产)
信号灯法
package Thread;
//测试生产者消费者问题2:信号灯法,标志位解决
public class TestPC2 {
public static void main(String[] args) {
TV tv = new TV();
new Player(tv).start();
new Watcher(tv).start();
}
}
// 生产者-->演员
class Player extends Thread{
TV tv = new TV();
public Player(TV tv)
{
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if (i%2==0)
{
this.tv.play("我是歌手播放中");
}
else
{
this.tv.play("蒙面歌王播放中");
}
}
}
}
// 消费者-->观众
class Watcher extends Thread{
TV tv = new TV();
public Watcher(TV tv)
{
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
this.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) {
throw new RuntimeException(e);
}
}
System.out.println("演员表演了:"+voice);
//通知观众观看
this.notify(); // 通知
this.voice = voice;
this.flag = !this.flag;
}
//观看
public synchronized void watch(){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("观看了:"+voice);
//通知演员表演
this.notify();
this.flag = !this.flag;
}
}
以演员和观众的例子写这个程序,首要的是演员和观众的类,如上所示,如例1所示,演员和观众类都继承了Thread类,这两个类中的run方法直接调用演出和观看的方法即可,这里面的产品对应的是节目类,例子1中是我们设置了缓冲区,而对于这个例子,我们把对缓冲区的操作改变,集成到产品类中,我们设置了一个标志位,演员表演,观众等待这就是true,反之就是false,然后分别写表演和观看的方法。
线程池
线程池这个概念非常简单,当我们创建或者说使用线程的时候,重复的这样子做其实是浪费资源和时间的,那不如就提前创建好,放在一个池子里,需要用的时候拿出来用,用完了在放回去,这其实节约了空间和资源。
上代码:
package Thread;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
// 测试线程池
public class TestPool {
public static void main(String[] args) {
//创建线程池
//newFixedThreadPool参数为【线程池大小】
ExecutorService service = Executors.newFixedThreadPool(10); //ExecutorService是线程池接口
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
//关闭连接
service.shutdown();
}
}
class MyThread implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
首先主函数中创建一个线程池,大小为10,采用Executors中的newFixedThreadPool方法,之后书写了四个线程,最后一定记得关闭连接即可。
总结:到此为止,线程的相关知识我已经讲完,接下来我个人会着重分享下集合、IO、常用类相关的知识,均为干货,大家可以收藏~