Skip to content

Instantly share code, notes, and snippets.

@noctarius
Created June 1, 2017 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noctarius/e29a693115ee9de01817119d0c42eaa1 to your computer and use it in GitHub Desktop.
Save noctarius/e29a693115ee9de01817119d0c42eaa1 to your computer and use it in GitHub Desktop.
package mc;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.core.ITopic;
import java.util.UUID;
public class MCDemo {
public static void main(String[] args) {
HazelcastInstance client = HazelcastClient.newHazelcastClient();
IMap<Integer, String> map = client.getMap("jax-2017");
ITopic<String> topic = client.getTopic("jax-2017");
topic.addMessageListener((m) -> {/*/dev/null*/});
new Thread(
() -> loop(() -> map.put(key(), value()))
).start();
new Thread(
() -> loop(() -> map.get(key()))
).start();
new Thread(
() -> loop(() -> map.remove(key()))
).start();
new Thread(
() -> loop(() -> topic.publish(UUID.randomUUID().toString()))
).start();
}
private static void sleep() {
try {
Thread.sleep((int) (Math.random() * 100));
} catch (Exception e) {}
}
private static int key() {
return (int) (Math.random() * 1000000);
}
private static String value() {
return UUID.randomUUID().toString();
}
private static void loop(Runnable runnable) {
while (true) {
runnable.run();
sleep();
}
}
private static void loop2(Runnable runnable) {
while (true) {
runnable.run();
//sleep();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment