3.310

这篇博客探讨了如何使用Java的`synchronized`、`wait()`和`notifyAll()`方法实现生产者消费者模型,展示了三种不同的线程创建方式:接口、继承和匿名内部类。此外,还对比了三种线程池类型:缓存线程池、单线程线程池和固定大小线程池的使用和特性。
摘要由CSDN通过智能技术生成
package com.lyl.day11;
//1、 使用synchronized 、wait() 、notifyAll()方法实现 生产者消费者模型
public class zuoye1 {
    private  final int Full=10;
    private  int count=0;//库存
    private  String z1="asd";
    class  Protuder implements Runnable{

        @Override
        public void run() {
            while (true){
                synchronized (z1){
            if (count==Full){
                try {
                    z1.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            count++;
                System.out.println("商品库存:"+count);
                z1.notifyAll();    }
        }
        }
    }
    class Consomer implements Runnable{

        @Override
        public void run() {
            while (true){
                synchronized (z1){
                    if (count==0)
                    {
                        try {
                            z1.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
            count--;
        z1.notifyAll();
            System.out.println("消费者使用,商品剩余:"+count);}
        }
        }
    }

    public static void main(String[] args) {
        zuoye1 z2=new zuoye1();
        Protuder p=z2.new Protuder();
        Consomer c=z2.new Consomer();
       Thread t1=new Thread(p);
       Thread t2=new Thread(c);
       t1.start();
       t2.start();
    }
}
//2、 使用3种不同的方式创建线程

//接口方式
public class zuoye02 implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
        }
    }
}

public class zuoye2 {
    public static void main(String[] args) {


//创建该类对象,并通过该类对象来调用Thread类型的对象
        zuoye02 z1 = new zuoye02();
        Thread t1 = new Thread(z1, "t1");
        t1.start();
    }
}



//继承方式
public class zuoye03 extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
        }
    }
}
public class zuoye2 {
public static void main(String[] args) {
    //创建该类的对象调用start方法。
    zuoye03 z1=new zuoye03();
    z1.start();

}
}
public class zuoye2 {


    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println(i);
                }
            }
        }).start();
    }

}

  3、 测试3种不同线程池,体会下区别

public static void main(String[] args) {
        //一个线程池,随便进,进来就有线程,有空闲就空闲没空闲就创建
    //    ExecutorService executorService= Executors.newCachedThreadPool();
        //单个 一个一个来
     //  ExecutorService executorService=Executors.newSingleThreadExecutor();
      // 固定几个线程,空闲则运行 否则就等待
        ExecutorService executorService= Executors.newFixedThreadPool(3);
        for (int i = 0; i < 10; i++) {
            final  int index=i;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+":"+index);
                }
            });
        }
    }
}
new新建时还未执行--新建状态
调用start方法,但还未执行,等待分配时间片--就绪状态
执行中---执行状态
当时间片用完,任务还未执行完毕则进入就绪状态
任务执行完毕- 消亡状态
d
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值