Java 等待唤醒机制

线程池

原理创建多个线程放在一个容器中  LinkedList  getfirst() 获取  使用完 再addlast()归还,达到反复利用
    java.util.concurrent.Executors:线程池的工厂类,用来生成线程池
    Executors类中的静态方法:
        1.static ExecutorService newFixedThreadPool(int nThreads) 创建一个可重用固定线程数的线程池
        2.参数:int nThreads:创建线程池中包含的线程数量
        3.返回值:ExecutorService接口,返回的是ExecutorService接口的实现类对象
        4. ExecutorService中的方法

             1. submit(Runnable task) 提交一个 Runnable 任务用于执行      2.关闭/销毁线程池的方法 void shutdown()

public class RunlabeImp implements Runnable {
    @Override
    public void run() {
        System.out.println("创建了一个线程"+Thread.currentThread().getName());
    }}
public class Demo01 {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(3);
        pool.submit(new RunlabeImp());
        pool.submit(new RunlabeImp());
        pool.submit(new RunlabeImp());
        pool.shutdown();       //4.调用ExecutorService中的方法shutdown销毁线程池(不建议执行)
    }} 

等待唤醒案例:线程之间的通信
        创建一个顾客线程(消费者):告知老板要的包子的种类和数量,调用wait方法,放弃cpu的执行,进入到WAITING状态(无限等待)
        创建一个老板线程(生产者):花了5秒做包子,做好包子之后,调用notify方法,唤醒顾客吃包子

    注意:
        顾客和老板线程必须使用同步代码块包裹起来,保证等待和唤醒只能有一个在执行
        同步使用的锁对象必须保证唯一
        只有锁对象才能调用wait和notify方法

    Obejct类中的方法
    void wait()
          在其他线程调用此对象的 notify() 方法或 notifyAll() 方法前,导致当前线程等待。
    void notify()
          唤醒在此对象监视器上等待的单个线程。  会继续执行wait方法之后的代码

public class Demo01WaitAndNotify {
    public static void main(String[] args) {
        Object obj = new Object();         //创建锁对象,保证唯一
        new Thread(){                      // 创建一个顾客线程(消费者)
            @Override
            public void run() {
               while(true){               //一直等着买包子
                   synchronized (obj){   //保证等待和唤醒的线程只能有一个执行,需要使用同步技术
                       System.out.println("告知老板要的包子的种类和数量");
                       try {
                           obj.wait();   //调用wait方法,放弃cpu的执行,进入到WAITING状态(无限等待)
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                       System.out.println("包子已经做好了,开吃!"); //唤醒之后执行的代码
                       System.out.println("---------------------------------------");
                   }}}}.start();

        new Thread(){         //创建一个老板线程(生产者)
            @Override
            public void run() {
                while (true){        //一直做包子
                    try {
                        Thread.sleep(5000);//花5秒钟做包子
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (obj){      //保证等待和唤醒的线程只能有一个执行,需要使用同步技术
                        System.out.println("老板5秒钟之后做好包子,告知顾客,可以吃包子了");
                        obj.notify();        //做好包子之后,调用notify方法,唤醒顾客吃包子
                    }}}}.start();}}

2.wait() 必须通过其他线程唤醒  wait(L 1000) 1秒后自己唤醒

3.如果有两个线程 在obj.wait()   notify() 随机唤醒一个 ,使用notify唤醒多个.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值