Skip to content

Instantly share code, notes, and snippets.

@mp911de
Created July 17, 2019 13:01
Show Gist options
  • Select an option

  • Save mp911de/22b12000c70caa912f4cf7fe7cf035f8 to your computer and use it in GitHub Desktop.

Select an option

Save mp911de/22b12000c70caa912f4cf7fe7cf035f8 to your computer and use it in GitHub Desktop.
<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