commit 7106caa0518df87682f93555103955dd8d74e84e
Author: chenchenxinhai <2793915820@qq.com>
Date: Sun Oct 29 15:57:47 2023 +0800
初始化
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5ff6309
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,38 @@
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### IntelliJ IDEA ###
+.idea/modules.xml
+.idea/jarRepositories.xml
+.idea/compiler.xml
+.idea/libraries/
+*.iws
+*.iml
+*.ipr
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store
\ No newline at end of file
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..35410ca
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
new file mode 100644
index 0000000..63574ec
--- /dev/null
+++ b/.idea/encodings.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..e2044d1
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml
new file mode 100644
index 0000000..2b63946
--- /dev/null
+++ b/.idea/uiDesigner.xml
@@ -0,0 +1,124 @@
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..4d785bd
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,58 @@
+
+
+ 4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.7.17
+
+
+ com.bwie
+ zuoye3
+ 0.0.1.1
+ zuoye3
+ zuoye3
+
+ 17
+
+
+
+ org.springframework.boot
+ spring-boot-starter
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+
+ redis.clients
+ jedis
+
+
+ junit
+ junit
+ test
+
+
+ redis.clients
+ jedis
+ 3.8.0
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+
+
diff --git a/src/main/java/com/bwie/pool/JedisPoolApplication.java b/src/main/java/com/bwie/pool/JedisPoolApplication.java
new file mode 100644
index 0000000..62e2498
--- /dev/null
+++ b/src/main/java/com/bwie/pool/JedisPoolApplication.java
@@ -0,0 +1,13 @@
+package com.bwie.pool;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class JedisPoolApplication {
+
+ public static void main (String[] args) {
+ SpringApplication.run (JedisPoolApplication.class, args);
+ }
+
+}
diff --git a/src/main/java/com/bwie/pool/service/RedisPool.java b/src/main/java/com/bwie/pool/service/RedisPool.java
new file mode 100644
index 0000000..fef75b9
--- /dev/null
+++ b/src/main/java/com/bwie/pool/service/RedisPool.java
@@ -0,0 +1,20 @@
+package com.bwie.pool.service;
+
+
+import redis.clients.jedis.Jedis;
+
+
+public interface RedisPool {
+
+
+ public void init (int maxTotal, long maxWaitMillis);
+
+
+
+ public Jedis getResource () throws Exception;
+
+
+
+
+ public void release (Jedis jedis);
+}
diff --git a/src/main/java/com/bwie/pool/service/impl/MyRedisPool.java b/src/main/java/com/bwie/pool/service/impl/MyRedisPool.java
new file mode 100644
index 0000000..c69a469
--- /dev/null
+++ b/src/main/java/com/bwie/pool/service/impl/MyRedisPool.java
@@ -0,0 +1,51 @@
+package com.bwie.pool.service.impl;
+
+
+import com.bwie.pool.service.RedisPool;
+import org.springframework.stereotype.Component;
+import redis.clients.jedis.Jedis;
+
+import java.util.concurrent.LinkedBlockingDeque;
+import java.util.concurrent.atomic.AtomicInteger;
+
+@Component
+public class MyRedisPool implements RedisPool {
+ private final AtomicInteger count = new AtomicInteger ();
+ private int maxTotal;
+ private long maxWaitMillis;
+ private LinkedBlockingDeque idleObjects = null;
+ private LinkedBlockingDeque activeObjects = null;
+
+ @Override
+ public void init (int maxTotal, long maxWaitMillis) {
+ this.maxTotal = maxTotal;
+ this.maxWaitMillis = maxWaitMillis;
+ idleObjects = new LinkedBlockingDeque<> (maxTotal);
+ activeObjects = new LinkedBlockingDeque<> (maxTotal);
+ }
+
+ @Override
+ public Jedis getResource () throws Exception {
+ long startTime = System.currentTimeMillis ();
+ Jedis jedis = idleObjects.poll ();
+ if (jedis != null) {
+ activeObjects.offer (jedis);
+ return jedis;
+ }
+ if (count.incrementAndGet () > maxTotal) {
+ count.decrementAndGet ();
+ throw new Exception ("连接数已满");
+ }
+ jedis = new Jedis ("127.0.0.1", 6379);
+ activeObjects.offer (jedis);
+ System.out.println ("---------------------------------------------创建了一个连接");
+ return jedis;
+ }
+
+ @Override
+ public void release (Jedis jedis) {
+ if (activeObjects.remove (jedis)) {
+ idleObjects.offer (jedis);
+ }
+ }
+}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/src/main/resources/application.properties
@@ -0,0 +1 @@
+
diff --git a/src/test/java/com.bwie.redis/JedisPoolApplicationTests.java b/src/test/java/com.bwie.redis/JedisPoolApplicationTests.java
new file mode 100644
index 0000000..58046c7
--- /dev/null
+++ b/src/test/java/com.bwie.redis/JedisPoolApplicationTests.java
@@ -0,0 +1,56 @@
+package com.bwie.redis;
+
+
+import com.bwie.pool.service.impl.MyRedisPool;
+import org.junit.jupiter.api.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+import redis.clients.jedis.Jedis;
+
+import javax.annotation.Resource;
+import java.util.concurrent.CountDownLatch;
+
+@RunWith (SpringRunner.class)
+@SpringBootTest
+class JedisPoolApplicationTests {
+ private final static int THREAD_NUM = 50;
+ private final CountDownLatch cdl = new CountDownLatch (THREAD_NUM);
+
+ @Resource
+ private MyRedisPool redisPool;
+
+ @Test
+ void contextLoads () throws Exception {
+ redisPool.init (45, 2000);
+
+ for (int i = 0; i < THREAD_NUM; i++) {
+ new Thread (() -> {
+ try {
+ System.out.println ("开始收集栅栏里的个体数" + THREAD_NUM);
+ cdl.await (); // 等待所有线程创建完毕
+ }
+ catch (Exception e) {
+ throw new RuntimeException (e);
+ }
+
+ Jedis jedis = null;
+
+ try {
+ jedis = redisPool.getResource ();
+ jedis.incr ("poolTest.inr");
+ }
+ catch (Exception e) {
+ System.out.println ("在运行时出现" + e.getMessage () + "异常");
+ }
+ finally {
+ redisPool.release (jedis);
+ }
+ }).start ();
+
+ cdl.countDown (); // 计数器减一,所有线程同时开始执行
+ }
+
+ Thread.sleep (2000); // 主线程休眠2秒,等待所有线程完成操作
+ }
+}