线程05线程通信

线程通信

多线程协同工作时需要通信,通信的方法

管程法

* @PackageName:Threadtest
 * @ClassName:pro_com_tee
 * @Description:使用管程法解决消费者与生产者的通信协作问题
 * @date 2021/8/12 9:05
 */

public class pro_com_tee {

    public static void main(String[] args) {
        Container container = new Container();
        new producer(container).start();
        new consumer(container).start();

    }


    static class producer extends Thread{
        Container container;

        public producer(Container container) {
            this.container = container;
        }

        @Override
        public void run() {

            for (int i = 0; i < 100; i++) {
                container.push(new Tea(i));
                System.out.println("生产了"+i+"杯奶茶");
            }

        }
    }

    static class consumer extends Thread{
        Container container;

        public consumer(Container container) {
            this.container = container;
        }

        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
              System.out.println("消费了"+container.pop().id+"杯奶茶");

            }

        }
    }

    static class Tea {
        int id;

        public Tea(int id) {
            this.id = id;
        }
    }


    static class Container {
       Tea[] teas = new Tea[10];
       int count = 0;

        //生产
        public synchronized void push(Tea tea){
            //判断容器是否满了
            if (count==teas.length) {
                //等待
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            teas[count]=tea;
            count++;
            //通知消费者消费
            this.notifyAll();
        }
        //消费
        public synchronized Tea pop(){
            //判断容器是否为空
            if (count==0){
                //等待,通知生产者生产
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            count--;
            Tea tea= teas[count];
            //消费了,可以生产了
            this.notifyAll();
            return tea;

        }

    }

信号灯法

通过标志位的变化来改变进程的状态来避免协作问题

线程池

 * @PackageName:Threadtest
 * @ClassName:ThreadPool
 * @Description:线程池的初级使用
 * @date 2021/8/12 20:11
 */

public class ThreadPool {
    public static void main(String[] args) {
        //创建服务,创建线程池
        //参数为池子大小
        ExecutorService service = Executors.newFixedThreadPool(10);
        //执行
        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());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值