java redis 实现多线程资源复用

java redis 实现多线程资源复用,第1张

java redis 实现多线程资源复用 1、测试类
package com.redis.resource;

import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@SpringBootTest
class ResourceApplicationTests {

    private static final int DEFAULT_RANGE_FOR_SLEEP = 2000;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Test
    void test() {
        String key = "list_cache_key";
        List list = Lists.newArrayList("A1", "A2", "A3", "A4", "A5");
        ListOperations operations = stringRedisTemplate.opsForList();
        stringRedisTemplate.delete(key);
        operations.leftPushAll(key, list);

        List listBefore = operations.range(key, 0, 100);
        System.out.println("当前资源:" + listBefore);

        //2、设置总请求数
        int num = 20;
        //3、设置并发资源数,须大于资源队列,才能体现出等待
        ExecutorService service = Executors.newFixedThreadPool(10);
        CountDownLatch latch = new CountDownLatch(num);

        for (int i = 0; i < num; i++) {
            int finalNum = i;
            service.execute(() -> {
                Integer retry = 0;
                Random r = new Random();
                boolean isRunning = true;
                try {
                    while (isRunning) {
                        if (retry > 10) {
                            throw new Exception("系统资源紧张,请稍后再试...");
                        }
                        System.out.println("线程:thread-" + finalNum + "从队列获取资源");
                        Object obj = operations.rightPop(key);

                        if (null == obj) {
                            System.out.println("线程:thread-" + finalNum + "未获取到资源,循环等待...");
                            Thread.sleep(1000);
                            retry++;
                            continue;
                        }

                        String data = obj.toString();

                        System.out.println("线程:thread-" + finalNum + "获取到资源:" + data);
                        Thread.sleep(r.nextInt(DEFAULT_RANGE_FOR_SLEEP));

                        operations.leftPush(key, data);
                        System.out.println("线程:thread-" + finalNum + "将资源" + data + "放回队列");
                        latch.countDown();
                        isRunning = false;

                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    //e.printStackTrace();
                }
            });
        }

        try {
            latch.await(num * 5000, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        List listAfter = operations.range(key, 0, 100);
        System.out.println("当前资源:" + listAfter);

        service.shutdown();
        System.out.println("进程结束,退出!!!");
        return;

    }

}

打印输出:

 当前资源:[A5, A4, A3, A2, A1]
线程:thread-0从队列获取资源
线程:thread-1从队列获取资源
线程:thread-2从队列获取资源
线程:thread-3从队列获取资源
线程:thread-4从队列获取资源
线程:thread-5从队列获取资源
线程:thread-6从队列获取资源
线程:thread-7从队列获取资源
线程:thread-8从队列获取资源
线程:thread-9从队列获取资源
线程:thread-4获取到资源:A2
线程:thread-7获取到资源:A1
线程:thread-3获取到资源:A3
线程:thread-2获取到资源:A4
线程:thread-6获取到资源:A5
线程:thread-5未获取到资源,循环等待...
线程:thread-8未获取到资源,循环等待...
线程:thread-0未获取到资源,循环等待...
线程:thread-9未获取到资源,循环等待...
线程:thread-1未获取到资源,循环等待...
线程:thread-7将资源A1放回队列
线程:thread-10从队列获取资源
线程:thread-10获取到资源:A1
线程:thread-2将资源A4放回队列
线程:thread-11从队列获取资源
线程:thread-11获取到资源:A4
线程:thread-11将资源A4放回队列
线程:thread-12从队列获取资源
线程:thread-12获取到资源:A4
线程:thread-8从队列获取资源
线程:thread-1从队列获取资源
线程:thread-0从队列获取资源
线程:thread-9从队列获取资源
线程:thread-5从队列获取资源
线程:thread-1未获取到资源,循环等待...
线程:thread-8未获取到资源,循环等待...
线程:thread-9未获取到资源,循环等待...
线程:thread-0未获取到资源,循环等待...
线程:thread-5未获取到资源,循环等待...
线程:thread-6将资源A5放回队列
线程:thread-13从队列获取资源
线程:thread-13获取到资源:A5
线程:thread-10将资源A1放回队列
线程:thread-12将资源A4放回队列
线程:thread-14从队列获取资源
线程:thread-15从队列获取资源
线程:thread-14获取到资源:A1
线程:thread-15获取到资源:A4
线程:thread-4将资源A2放回队列
线程:thread-16从队列获取资源
线程:thread-16获取到资源:A2
线程:thread-3将资源A3放回队列
线程:thread-17从队列获取资源
线程:thread-17获取到资源:A3
线程:thread-14将资源A1放回队列
线程:thread-18从队列获取资源
线程:thread-18获取到资源:A1
线程:thread-5从队列获取资源
线程:thread-0从队列获取资源
线程:thread-8从队列获取资源
线程:thread-9从队列获取资源
线程:thread-1从队列获取资源
线程:thread-0未获取到资源,循环等待...
线程:thread-8未获取到资源,循环等待...
线程:thread-5未获取到资源,循环等待...
线程:thread-9未获取到资源,循环等待...
线程:thread-1未获取到资源,循环等待...
线程:thread-16将资源A2放回队列
线程:thread-19从队列获取资源
线程:thread-19获取到资源:A2
线程:thread-17将资源A3放回队列
线程:thread-13将资源A5放回队列
线程:thread-8从队列获取资源
线程:thread-5从队列获取资源
线程:thread-0从队列获取资源
线程:thread-1从队列获取资源
线程:thread-9从队列获取资源
线程:thread-1未获取到资源,循环等待...
线程:thread-5未获取到资源,循环等待...
线程:thread-9未获取到资源,循环等待...
线程:thread-8获取到资源:A3
线程:thread-0获取到资源:A5
线程:thread-15将资源A4放回队列
线程:thread-18将资源A1放回队列
线程:thread-19将资源A2放回队列
线程:thread-0将资源A5放回队列
线程:thread-1从队列获取资源
线程:thread-9从队列获取资源
线程:thread-5从队列获取资源
线程:thread-1获取到资源:A4
线程:thread-9获取到资源:A1
线程:thread-5获取到资源:A2
线程:thread-5将资源A2放回队列
线程:thread-8将资源A3放回队列
线程:thread-9将资源A1放回队列
线程:thread-1将资源A4放回队列
当前资源:[A4, A1, A3, A2, A5]
进程结束,退出!!!

2、application.yml
spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password: 123123
    timeout: 5000ms
  pool:
    max-active: 8
    min-idle: 0
    max-idle: 8
    max-wait: -1
3、pom.xml


	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.6.2
		 
	
	com.redis
	resource
	0.0.1-SNAPSHOT
	resource
	Demo project for Spring Boot
	
		1.8
	
	
		
			org.springframework.boot
			spring-boot-starter-data-redis
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


代码示例下载地址:resource: redis资源复用

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5707424.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存