• Condition


    Lock,那是对synchronized的一种更为面向对象的替代,在原来的synchronized内部,我们可以调用object的wait与notify方法

    condition一方面是对lock功能的补充(也就是说,你用了lock,为了保证线程的通信,就得用condition)

    另一方面,synchronized的notifyall是唤醒所有等待的线程,那么如果有些线程我不想唤醒呢。

    下面的例子:主线程运行10次,然后子线程2运行20次,接着子线程3运行30次

    import java.util.concurrent.atomic.AtomicInteger;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class ThreeConditionCommunication {
    
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    
            final Business business = new Business();
            new Thread(
                    new Runnable() {
    
                        @Override
                        public void run() {
    
                            for (int i = 1; i <= 4; i++) {
                                business.sub2(i);
                            }
    
                        }
                    }
            ).start();
    
            new Thread(
                    new Runnable() {
    
                        @Override
                        public void run() {
    
                            for (int i = 1; i <= 4; i++) {
                                business.sub3(i);
                            }
    
                        }
                    }
            ).start();
    
            for (int i = 1; i <= 4; i++) {
                business.main(i);
            }
    
        }
    
    
        static class Business {
            Lock lock = new ReentrantLock();
            Condition condition1 = lock.newCondition();
            Condition condition2 = lock.newCondition();
            Condition condition3 = lock.newCondition();
            private int shouldSub = 1;
    
            public void sub2(int i) {
                lock.lock();
                try {
                    while (shouldSub != 2) {
                        try {
                            condition2.await();
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    for (int j = 1; j <= 20; j++) {
                        System.out.println("sub2 thread sequence of " + j + ",loop of " + i);
                    }
                    shouldSub = 3;
                    condition3.signal();
                } finally {
                    lock.unlock();
                }
            }
    
    
            public void sub3(int i) {
                lock.lock();
                try {
                    while (shouldSub != 3) {
                        try {
                            condition3.await();
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    for (int j = 1; j <= 30; j++) {
                        System.out.println("sub3 thread sequence of " + j + ",loop of " + i);
                    }
                    shouldSub = 1;
                    condition1.signal();
                } finally {
                    lock.unlock();
                }
            }
    
            public void main(int i) {
                lock.lock();
                try {
                    while (shouldSub != 1) {
                        try {
                            condition1.await();
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    for (int j = 1; j <= 10; j++) {
                        System.out.println("main thread sequence of " + j + ",loop of " + i);
                    }
                    shouldSub = 2;
                    condition2.signal();
                } finally {
                    lock.unlock();
                }
            }
    
        }
    }
  • 相关阅读:
    一个C++程序员学习C#语言
    C#入门教程笔记
    完全卸载mysql 停止服务、卸载相关程序、删除注册表
    C++结构简介
    babun,windows shell
    无限极设计以及随意移动节点(树结构)
    springboot 配置访问外部静态资源详解
    mysql8+keepalived 双主高可用搭建
    mysql 双主复制搭建
    mysql 主备搭建
  • 原文地址:https://www.cnblogs.com/tinya/p/8452791.html
Copyright © 2020-2023  润新知