zoukankan      html  css  js  c++  java
  • java ReentrantReadWriteLock

    // read and write lock is mutual exclusion lock
    //Listing 7-3. Using ReadWriteLock to Satisfy a Dictionary Application’s Reader and
    //Writer Threads
    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReadWriteLock;
    import java.util.concurrent.locks.ReentrantReadWriteLock;
    public class A
    {
        public static void main(String[] args)
        {
            final String[] words =
            {
            "hypocalcemia",
            "prolixity",
            "assiduous",
            "indefatigable",
            "castellan"
            };
            final String[] definitions =
            {
            "a deficiency of calcium in the blood",
            "unduly prolonged or drawn out",
            "showing great care, attention, and effort",
            "able to work or continue for a lengthy time without tiring",
            "the govenor or warden of a castle or fort"
            };
            
            final Map<String, String> dictionary = new HashMap<String, String>();
            ReadWriteLock rwl = new ReentrantReadWriteLock(true);
            final Lock rlock = rwl.readLock();
            final Lock wlock = rwl.writeLock();
            Runnable writer = () ->
            {
                    for (int i = 0; i < words.length; i++)
                    {
                        wlock.lock();
                        try
                        {
                            dictionary.put(words[i],definitions[i]);
                            System.out.println("writer storing " +words[i] + " entry");
                        }
                        finally
                        {
                            wlock.unlock();
                        }
                        
                        try
                        {
                        Thread.sleep(1);
                        }
                        catch (InterruptedException ie)
                        {
                        System.err.println("writer " +
                        "interrupted");
                        }
                    }
            };
            
            ExecutorService es = Executors.newFixedThreadPool(1);
            es.submit(writer);
            
            Runnable reader = () ->
            {
                while (true)
                {
                        rlock.lock();
                        try
                        {
                            int i = (int) (Math.random() *    words.length);
                                    System.out.println("reader accessing " + words[i] + ": " +dictionary.get(words[i])
                                    + " entry");
                        }
                        finally
                        {
                        rlock.unlock();
                        }
                }
             };
                        
        es = Executors.newFixedThreadPool(1);
        es.submit(reader);
        }
    }
  • 相关阅读:
    Web Service接口设计(转)
    DataTable绑定到GridView时,RowDataBound事件
    SQLiteHelper
    SQL FOR XML子句的各种用法
    公历转农历函数
    SQL里变量的声明以及常用函数举例
    Python 与 Matlab混合语言编程资料
    Iterator和Generator学习心得(二)转
    python26 调用mysql 5.1
    转:程序员能力矩阵
  • 原文地址:https://www.cnblogs.com/rojas/p/5381732.html
Copyright © 2011-2022 走看看