我正在使用 redis 存储一组有序的项目。这是一个示例代码:
object Producer{
def main(args:Array[String]){
val jedis = new Jedis("localhost")
for (i<-1 to 10){
println("publishing:"+(i))
jedis.lpush("q1",i.toString)
}
}
}
object Consumer {
def main(args:Array[String]){
val jedis = new Jedis("localhost")
while(true){
while(jedis.llen("q1")>0){
val msg=jedis.lpop("q1")
println("processing:"+msg)
}
}
}
}
当我运行生产者时,我得到
publishing:1
publishing:2
publishing:3
publishing:4
publishing:5
publishing:6
publishing:7
publishing:8
publishing:9
publishing:10
当我运行消费者时,我得到
processing:1
processing:2
processing:4
processing:5
processing:6
processing:7
processing:9
processing:10
processing:8
processing:3
为什么项目顺序不正确,这不是真正的先进先出。