• java并发:Condition的应用


    Condition类可以使线程等待,也可以唤醒线程。
    Condition类的await方法和Object类的wait方法等效
    Condition类的signal方法和Object类的notify方法等效
    Condition类的signalAll方法和Object类的notifyAll方法等效

    注意:awit(),signal(),signalAll()这些方法,都必须先进行线程同步后,才可以使用,否则会报错:IllegalMonitorStateException
    示例如下:

    public class ConditionDemo {
       private  static Lock lock=new ReentrantLock();
       private static Condition condition=lock.newCondition();
    
        public static void main(String[] args)   {
             ThreadSignal threadSignal =new ThreadSignal();
             threadSignal.start();
                 
              try {
                System.out.println("在主线程中利用Condition阻塞线程 :"+ Thread.currentThread().getName());
                condition.await();
               }catch (InterruptedException e) {
                      e.printStackTrace();
              }
        }
    
      static class ThreadSignal extends  Thread {
    
          @Override
          public void run() {
              try {
                  lock.lock();
                  System.out.println("在线程"+Thread.currentThread().getName()+"中加锁");
                  System.out.println("在线程"+Thread.currentThread().getName()+"中准备唤醒。");
                  condition.signal();
    
              }catch (Exception e) {
                   e.printStackTrace();
              }finally {
                   lock.unlock();
                   System.out.println("在线程"+Thread.currentThread().getName()+"中解锁。");
              }
          }
      }
    
    
    }

    以上代码在主线程中,没有进行同步或加锁,就直接使用Condition的await()方法,会出错IllegalMonitorStateException。

    需要先进行加锁,再进入等待,修改如下:

    public class ConditionDemo {
       private  static Lock lock=new ReentrantLock();
       private static Condition condition=lock.newCondition();
    
        public static void main(String[] args)   {
             ThreadSignal threadSignal =new ThreadSignal();
             threadSignal.start();
    
             try {
                 lock.lock();
                 System.out.println("在主线程中加锁。");
                 System.out.println("在主线程中利用Condition阻塞线程 :"+ Thread.currentThread().getName());
                 condition.await();
             }catch (InterruptedException e) {
                 e.printStackTrace();
             }finally {
                 lock.unlock();
                 System.out.println("在主线程中解锁。");
             }
    
        }
    
    
      static class ThreadSignal extends  Thread {
    
          @Override
          public void run() {
              try {
                  lock.lock();
                  System.out.println("在线程"+Thread.currentThread().getName()+"中加锁");
                  condition.signal();
                  System.out.println("在线程"+Thread.currentThread().getName()+"中唤醒。");
              }catch (Exception e) {
                   e.printStackTrace();
              }finally {
                   lock.unlock();
                   System.out.println("在线程"+Thread.currentThread().getName()+"中解锁。");
              }
          }
      }
    
    
    }

    运行结果如下:

    在主线程中加锁。
    在主线程中利用Condition使线程等待。
    在线程Thread-0中加锁
    在线程Thread-0中唤醒。
    在主线程中解锁。
    在线程Thread-0中解锁。
  • 相关阅读:
    display:flex 布局之 骰子
    vue 生命周期
    vue webpack 懒加载
    后台管理页面基本布局
    模拟ie9的placeholder
    常用的功能封装 pool.js
    六位数字字母验证码
    CommonJs AMD CMD
    项目封版后的总结
    jq 回到顶部
  • 原文地址:https://www.cnblogs.com/expiator/p/9376779.html
Copyright © 2020-2023  润新知