并发 – 在Redis出列的“停止等待”状态中的线程

我有一个运行多个线程的tomcat –
spring4.2应用程序.每个线程仅从一个队列中出队,但是有多个线程分配给队列.

事情开始很好,但是经过几个小时/ ~500k的出列操作,我发现线程以极慢的速度出列.

在jvisualvm中,我看到橙色的线程即公园
线程转储如下:

"EMLT_2" - Thread t@64
   java.lang.Thread.State: WAITING
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for <2cf42d7> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
    at org.apache.commons.pool2.impl.LinkedBlockingDeque.takeFirst(LinkedBlockingDeque.java:583)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:442)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)
    at redis.clients.util.Pool.getResource(Pool.java:48)
    at redis.clients.jedis.JedisPool.getResource(JedisPool.java:86)
    at com.mycomp.sam.processors.SimpleDequeuer.dequeue(SimpleDequeuer.java:25)
    at com.mycomp.sam.processors.EMLT.run(EMLT.java:29)
    at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
    - None

"EMLT_1" - Thread t@63
   java.lang.Thread.State: WAITING
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for <2cf42d7> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
    at org.apache.commons.pool2.impl.LinkedBlockingDeque.takeFirst(LinkedBlockingDeque.java:583)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:442)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)
    at redis.clients.util.Pool.getResource(Pool.java:48)
    at redis.clients.jedis.JedisPool.getResource(JedisPool.java:86)
    at com.mycomp.sam.processors.SimpleDequeuer.dequeue(SimpleDequeuer.java:25)
    at com.mycomp.sam.processors.EMLT.run(EMLT.java:29)
    at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
    - None

出列方法是:

public String dequeue(String queue) {
        try (Jedis jedis = jedispool.getResource()) {
            List<String> str = jedis.blpop(10, queue);
            if(str!=null){
                return str.get(1);
            }
            else 
                return null; 
        }
    }

会欣赏投入.该应用程序再次运行良好一段时间后重新启动.
池配置:

<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxIdle" value="10" />
    <property name="maxTotal" value="70" />
    <property name="minIdle" value="10" />
</bean>
<bean id="jedispool" class="redis.clients.jedis.JedisPool">
    <constructor-arg name="poolConfig" ref="poolConfig" />
    <constructor-arg name="host" value="${REDIS_HOST}" />
    <constructor-arg name="port" value="6379" />
</bean>

最佳答案 您的jedis池似乎没有连接.

您是使用returnResourceObject还是returnResource返回资源? (我知道两者都被弃用了,但它们仍然适用于jedis的lates版本).

请记住,jedis池与DBCP / Apache池不完全相同.

我有一个类似的问题,当我调用上面的方法时,它结束了.

另一方面,由于Redis非常快,也许你应该重新考虑你的模式,并且很少有线程调用blpop(每个队列一个)并将值转发给其他线程,所以那些人不知道redis不必.

点赞