zoukankan      html  css  js  c++  java
  • java Conditions

    //Listing 7-2. Achieving Synchronization in Terms of Locks and Conditions
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class A {
        public static void main(String[] args) {
            Shared s = new Shared();
            new Producer(s).start();
            new Consumer(s).start();
        }
    }
    
    class Shared {
        private char c;
        private volatile boolean available;
        private final Lock lock;
        private final Condition condition;
    
        Shared() {
            available = false;
            lock = new ReentrantLock();
            condition = lock.newCondition();
        }
    
        Lock getLock() {
            return lock;
        }
    
        char getSharedChar() {
            lock.lock();
            try {
                while (!available)
                    try {
                        condition.await();
                    } catch (InterruptedException ie) {
                        ie.printStackTrace();
                    }
                available = false;
                condition.signal();
            } finally {
                lock.unlock();
                return c;
            }
        }
    
        void setSharedChar(char c) {
            lock.lock();
            try {
                while (available)
                    try {
                        condition.await();
                    } catch (InterruptedException ie) {
                        ie.printStackTrace();
                    }
                this.c = c;
                available = true;
                condition.signal();
            } finally {
                lock.unlock();
            }
        }
    }
    
    class Producer extends Thread {
        private final Lock l;
        private final Shared s;
    
        Producer(Shared s) {
            this.s = s;
            l = s.getLock();
        }
    
        @Override
        public void run() {
            for (char ch = 'A'; ch <= 'Z'; ch++) {
                l.lock();
                s.setSharedChar(ch);
                System.out.println(ch + " produced by producer.");
                l.unlock();
            }
        }
    }
    
    class Consumer extends Thread {
        private final Lock l;
        private final Shared s;
    
        Consumer(Shared s) {
            this.s = s;
            l = s.getLock();
        }
    
        @Override
        public void run() {
            char ch;
            do {
                l.lock();
                ch = s.getSharedChar();
                System.out.println(ch + " consumed by consumer.");
                l.unlock();
            } while (ch != 'Z');
        }
    }
  • 相关阅读:
    zabbix服务端安装部署
    SQL基础术语和单行函数
    Win 2008 R2——由于管理员设置的策略,该磁盘处于脱机状态
    如何扎实自己的Java基础?
    可任意拖拽的div js 代码
    最新版通过前端js 代码实现html转canvas载转成pdf的方法
    spring四大注解
    百度地图API:自定义多个途经点的导航
    用jrebel实现 jvm热部署,修改类不用重启tomcat
    jsp 转为pdf
  • 原文地址:https://www.cnblogs.com/rojas/p/5381316.html
Copyright © 2011-2022 走看看