修改bug

master
chenchenxinhai 2023-10-29 19:11:04 +08:00
parent 7106caa051
commit be7772dd89
3 changed files with 52 additions and 57 deletions

View File

@ -1 +0,0 @@

View File

@ -1,56 +0,0 @@
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秒等待所有线程完成操作
}
}

View File

@ -0,0 +1,52 @@
package com.bwie.pol;
import com.bwie.pool.service.impl.MyRedisPool;
import org.junit.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(classes = MyRedisPool.class)
public class JedisPoolApplicationTests {
private final static int THREAD_NUM=50;
private final CountDownLatch cdl = new CountDownLatch(THREAD_NUM);
@Resource
public MyRedisPool redisPool;
@Test
public 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();
}
// 主线程休眠2秒等待所有线程完成操作
Thread.sleep(2000);
}
}