redis-计数信号量

Posted 手握太阳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis-计数信号量相关的知识,希望对你有一定的参考价值。

1、基本概念

2、信号量类

3、测试类

4、测试日志

 

基本概念

 

计数信号量是一种锁,它可以让用户限制一项资源最多能够同时被多少个进程访问,

技术信号量和其他锁的区别:当客户端获取锁失败时,客户端会选择等待;而获取信号量失败时,通常直接退出,并向用户提示“资源繁忙”,由用户决定下一步如何处理。

 

信号量类

import java.util.UUID;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;

public class Semaphore_Lock {
    public static String acquire_semaphore(Jedis redis,String sem_name,int limit,long timeout){
        String identifier=UUID.randomUUID().toString();
        long now=System.currentTimeMillis();
        Pipeline pipeline=redis.pipelined();
        //清理其他持有者过期信号量
        pipeline.zremrangeByScore(sem_name, 0, now-timeout);
        pipeline.zadd(sem_name,now,identifier);
        Response<Long> rank=pipeline.zrank(sem_name, identifier);
        pipeline.syncAndReturnAll();
        if((Long)rank.get()<limit){
            System.out.println(Thread.currentThread().getName()+"  identifier rank :"+rank.get());
            return identifier;
        }else{
            System.out.println(Thread.currentThread().getName()+"  identifier rank :"+rank.get()+",too late");
        }
        redis.zrem(sem_name, identifier);
        return null;
    }
    public static Long release_semaphore(Jedis redis,String sem_name,String identifier){
        return redis.zrem(sem_name, identifier);
    }
}


测试类

import redis.clients.jedis.Jedis;

public class TestSemaphore_Lock {
    public static void main(String[] args) {
        System.out.println( " main start");
        for (int i = 0; i < 1000; i++) {
            Test_Thread_lock threadA = new Test_Thread_lock();
            threadA.start();
            try {
               // Thread.sleep(1);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

class Test_Thread_lock extends Thread {

    public void run() {
        System.out.println(Thread.currentThread().getName() + " 启动运行");
        Jedis redis=JedisUtil.getJedis();
        while(true){
            if(redis==null){
                redis=JedisUtil.getJedis();
                System.out.println(Thread.currentThread().getName() + " 获取redis连接失败,等待几秒后,继续尝试获取.conn conn  conn");
                try {
                    sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                continue;
            }
            String indentifier = Semaphore_Lock.acquire_semaphore(redis, "sem_name", 20, 1000*60);
            if (indentifier == null) {
                System.out.println(Thread.currentThread().getName() + " 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore");
            }else{
                System.out.println(Thread.currentThread().getName() + "  执行任务");
                try {
                    sleep(10);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Semaphore_Lock.release_semaphore(redis,"sem_name", indentifier);
                redis.close();
                break;
            }
        }
    }

}

 

测试日志

Thread-274  identifier rank :22,too late
Thread-255  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-213  identifier rank :23,too late
Thread-255 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-213 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-255  identifier rank :22,too late
Thread-213  identifier rank :21,too late
Thread-274  identifier rank :21,too late
Thread-255 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-213 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-255  identifier rank :21,too late
Thread-213  identifier rank :21,too late
Thread-274  identifier rank :21,too late
Thread-255 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-213 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-255  identifier rank :20,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-213  identifier rank :21,too late
Thread-255 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-255  identifier rank :20,too late
Thread-213 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-255 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-213  identifier rank :19
Thread-213  执行任务
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-255  identifier rank :19
Thread-255  执行任务
Thread-274  identifier rank :20,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :20,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :21,too late
Thread-274 获取信号量失败,取消任务. Semaphore  Semaphore  Semaphore  Semaphore
Thread-274  identifier rank :18
Thread-274  执行任务
Thread-963  identifier rank :16
Thread-963  执行任务

 


以上是关于redis-计数信号量的主要内容,如果未能解决你的问题,请参考以下文章

redis计数器,怎么样保证取值正确

Curator实现分布式锁(可重入 不可重入 读写 联锁 信号量 栅栏 计数器)

ios开发GCD-dispatch_semaphore_t信号量计数器

Linux-信号量-线程池

信号量 - 初始计数有啥用?

Redis在电商中的实际应用-Java