Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
public class MultithreadedExample {
public static void main(String[] args) throws Exception{
// Syntax: redis://[password@]host[:port][/databaseNumber]
RedisClient redisClient = RedisClient.create(RedisURI.create("redis://localhost:6379/0"));
CountDownLatch latch = new CountDownLatch(1);
List<RedisThread> threads = IntStream.range(0, 100).mapToObj(ignore -> new RedisThread(redisClient, latch))
.collect(Collectors.toList());
threads.forEach(Thread::start);
Thread.sleep(100);
latch.countDown();
threads.forEach(redisThread -> {
try {
redisThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
redisClient.shutdown();
}
static class RedisThread extends Thread{
StatefulRedisConnection<String, String> connection;
private final CountDownLatch latch;
public RedisThread(RedisClient client, CountDownLatch latch) {
this.connection = client.connect();
this.latch = latch;
}
@Override
public void run() {
System.out.println("Arm");
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
return;
}
System.out.println("Go!");
for (int i = 0; i < 10; i++) {
connection.async().eval(…);
}
connection.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment