jedis connection exception unexpected end of stream

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/u013041642/article/details/80771122

jedis connection exception unexpected end of stream

多线程的时候,我的代码起初是这样子的:

ExecutorService pool = Executors.newFixedThreadPool(100); // 线程池100
Jedis jedis = jedisPool.getResource();
for (int i = 0; i < 100; i++) {
  pool.execute(() -> {
    // 使用上面的jedis
  });
}

前一两个线程执行还能成功,但是到后面就全部失败了,报上面的错误。

并发的时候, 同一个jedis实例给所有线程共享的话,可能上一个线程还没释放连接,另一个连接就开始用了,这个时候就会有edis connection exception unexpected end of stream

后来把代码改成:

ExecutorService pool = Executors.newFixedThreadPool(100); // 线程池100
for (int i = 0; i < 100; i++) {
  pool.execute(() -> {
    Jedis jedis = jedisPool.getResource();
    // 做一些操作
    jedis.close();
  });
}

就可以了。

猜你喜欢

转载自blog.csdn.net/u013041642/article/details/80771122