模拟英雄联盟游戏盒子的英雄排行榜的实现
代码实现:
static class HeroScoreGenerate implements Runnable{
public void run() {
String[] heros = new String[]{"盲僧","稻草人","盖伦","机器人","亚索","锐雯","剑圣","卡萨丁"};
Random random = new Random();
Jedis jedis = new Jedis("192.168.90.131");
while(true){
try {
Thread.sleep(100+random.nextInt(10)*100);
int index = random.nextInt(8);
String hero = heros[index];
jedis.zincrby("TopRankChuChang",1,hero);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new Thread(new HeroScoreGenerate()).start();
Jedis jedis = new Jedis("192.168.90.131");
int i = 1;
while(true){
try {
Thread.sleep(3000);
Set<Tuple> heros = jedis.zrevrangeWithScores("TopRankChuChang", 0, -1);
System.out.printf("第%s次获取排行榜:",i);
System.out.println();
for(Tuple t:heros){
System.out.println(t.getElement()+":"+t.getScore());
}
i++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}