编程语言
首页 > 编程语言> > java开发的第十八个博客(线程通信与线程池)

java开发的第十八个博客(线程通信与线程池)

作者:互联网

1.线程通信

1.线程通信概念

2.为什么要进行线程通信

3.如何通过线程通信使多个线程实现有效协作

2.等待唤醒机制

1.概念

2.等待唤醒方法

3.注意点

4.细节

5.生产者与消费者案例

import java.util.Random;

public class demo8 {
    public static void main(String[] args) {
        BaoZi bz = new BaoZi();
        BaoZiPu bzp = new BaoZiPu("包子铺",bz);
        ChiHuo ch = new ChiHuo("吃货", bz);
        bzp.start();
        ch.start();
    }
}
class BaoZi{//包子类
    private String pu;
    private String xian;
    private boolean flag;
    public String getPu() {
        return pu;
    }

    public String getXian() {
        return xian;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setPu(String pu) {
        this.pu = pu;
    }

    public void setXian(String xian) {
        this.xian = xian;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}
class BaoZiPu extends Thread{//包子铺类
    private BaoZi bz;
    private int count;
    Random r = new Random();
    public BaoZiPu(String bzp, BaoZi bz) {
        super(bzp);
        this.bz = bz;
    }

    @Override
    public void run() {//生产包子
        while (true){
            synchronized (bz){
                if(bz.isFlag() == false){
                    if(r.nextInt(2)==0){
                        bz.setPu("面皮");
                        bz.setXian("韭菜");
                    }else {
                        bz.setPu("饺子皮");
                        bz.setXian("猪肉");
                    }
                    System.out.println(this.getName()+"做出了"+bz.getPu()+bz.getXian()+"包子");
                    bz.setFlag(true);
                    bz.notify();
                }else {
                    try {
                        bz.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            if(++count==100){
                break;
            }
        }
    }
}
class ChiHuo extends Thread {//吃货类
    private BaoZi bz;
    private int count;
    public ChiHuo(String ch, BaoZi bz) {
        super(ch);
        this.bz = bz;
    }
    @Override
    public void run() {//负责吃包子
        while (true) {
            synchronized (bz) {
                if (bz.isFlag() == true) {
                    System.out.println(this.getName() + "吃了" + bz.getPu() + bz.getXian() + "包子");
                    bz.setFlag(false);
                    bz.notify();
                } else {
                    try {
                        bz.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            if(++count==100){
                break;
            }
        }
    }
}

3.线程池

1.由来

2.概念

3.优点

4.线程池的使用

5.代码实例

import java.util.concurrent.*;
public class demo8 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
         A a = new A();//
        ExecutorService ex = Executors.newFixedThreadPool(1);//创建线程池对象
        Future<Boolean> submit = ex.submit(a);//执行线程并提交执行结果给Future类对象
        Boolean b = submit.get();//Future类对象获取返回值并接收
        ex.shutdownNow();//关闭线程池
    }
}
class A implements Callable<Boolean>{
    @Override
    public Boolean call() throws Exception {
        return true;//重写call方法
    }
}

标签:第十八个,java,String,void,线程,唤醒,public,bz
来源: https://blog.csdn.net/adminsyso/article/details/112294042