我很困惑:
用法:try(Pipeline pipeline = jedisPool.getResource().pipelined())
它将关闭管道和jedis实例吗?还是只是关闭管道?
我将此与多线程一起使用,但出现以下错误。我感到困惑的是,错误是由于JedisPool中的实例用完(由于使用try-with-resource引起的)还是死锁(因为JedisPool在多线程中表现不佳)引起的?
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)
org.apache.commons.pool2.impl.LinkedBlockingDeque.takeFirst(LinkedBlockingDeque.java:583)
org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:442)
org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)
redis.clients.util.Pool.getResource(Pool.java:49)
redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
io.codis.jodis.RoundRobinJedisPool.getResource(RoundRobinJedisPool.java:214)
最佳答案
Try-with-resources仅关闭变量,在您的情况下为Pipeline pipeline
。您可以在文档https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html中阅读更多内容
您可以使用以下示例进行检查:MyResource
类:
class MyResource implements AutoCloseable {
public SubResource getSubResource() {
return new SubResource();
}
@Override
public void close() throws Exception {
System.out.println("Resource closed");
}
}
SubResource
类:class SubResource implements AutoCloseable{
@Override
public void close() throws Exception {
System.out.println("SubResource closed");
}
}
Main
类:class Main {
public static void main(String[] args) {
try (SubResource s = new MyResource().getSubResource()) {
System.out.println("Before closing");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("After closing");
}
}
执行结果:
Before closing
SubResource closed
After closing
关于java - 在JedisPool中尝试使用链接资源,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45166674/