Java多线程之《读写锁》

读写锁实现缓存示例:

package concurrent;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Desc: 缓存示例,读写锁实现
 * Creator: pengweixiang
 * Date: 2019-04-21
 */
public class Cache
{
    private static Map<String, String> map = new HashMap<>();

    private static ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
    private static Lock r = rwl.readLock();
    private static Lock w = rwl.writeLock();

    /**
     * 获取对应key的值
     * @param key key
     * @return value
     */
    public static String get(String key)
    {
        r.lock();
        try
        {
            return map.get(key);
        }
        finally
        {
            r.unlock();
        }
    }

    /**
     * 设置key对应的新值,并返回旧的值
     * @param key key
     * @param value newValue
     * @return oldValue
     */
    public static String put(String key, String value)
    {
        w.lock();
        try
        {
            return map.put(key, value);
        }
        finally
        {
            w.unlock();
        }
    }

    /**
     * 清空缓存的所有值
     */
    public static void clearCache()
    {
        w.lock();
        try
        {
            map.clear();
        }
        finally
        {
            w.unlock();
        }
    }
}

测试代码:

package concurrent;

import org.junit.Test;


/**
 * Desc: 读写锁Cache测试类
 * Creator: pengweixiang
 * Date: 2019-04-21
 */
public class CacheTest
{
    @Test
    public void test()
    {
        for (int i = 0; i < 2; i++)
        {
            String threadName = "thread" + i;
            ReadTask readTask = new ReadTask(threadName);
            WriteTask writeTask = new WriteTask(threadName);
            readTask.setDaemon(true);
            writeTask.setDaemon(true);
            writeTask.start();
            readTask.start();
        }

        TimeUtils.sleep(20);
    }

    private static class ReadTask extends Thread
    {
        private String threadName;

        public ReadTask(String name)
        {
            super(name);
            threadName = name;
        }

        @Override
        public void run()
        {
            while (true)
            {
                String value = (String) Cache.get(threadName);
                System.out.println(Thread.currentThread().getName() + ", read value: " + value);
                TimeUtils.sleep(1);
            }
        }
    }

    private static class WriteTask extends Thread
    {
        private String threadName;

        public WriteTask(String name)
        {
            super(name);
            threadName = name;
        }

        @Override
        public void run()
        {
            while (true)
            {
                String value = (String) Cache.put(threadName, TimeUtils.getCurrentTime());
                System.out.println(Thread.currentThread().getName() + ", write value: " + value);
                TimeUtils.sleep(1);
            }
        }
    }
}

github地址:https://github.com/thinkerpeng/Java-Concurrent

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值