Created
August 30, 2012 08:24
-
-
Save psfung/3524148 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mm; | |
import java.util.UUID; | |
import com.hazelcast.config.ClasspathXmlConfig; | |
import com.hazelcast.core.Hazelcast; | |
import com.hazelcast.core.HazelcastInstance; | |
import com.hazelcast.core.MultiMap; | |
public class Main { | |
public static void main(String[] args) { | |
HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(new ClasspathXmlConfig("mm.xml")); | |
MultiMap<Integer, UUID> map = hazelcast.getMultiMap("mm"); | |
Putter putter = new Putter(map); | |
Remover remover = new Remover(map); | |
Thread putterThread = new Thread(putter); | |
Thread removerThread = new Thread(remover); | |
putterThread.start(); | |
removerThread.start(); | |
try { | |
Thread.sleep(10000); | |
} catch (InterruptedException e) {} | |
putter.setRunning(false); | |
try { | |
putterThread.join(); | |
} catch (InterruptedException e) {} | |
remover.setRunning(false); | |
try { | |
removerThread.join(); | |
} catch (InterruptedException e) {} | |
try { | |
Thread.sleep(2000); | |
} catch (InterruptedException e) {} | |
hazelcast.getLifecycleService().shutdown(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-basic.xsd" | |
xmlns="http://www.hazelcast.com/schema/config" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |
<group> | |
<name>mm</name> | |
<password>mm</password> | |
</group> | |
<network> | |
<port auto-increment="true">5701</port> | |
<join> | |
<multicast enabled="true"> | |
<multicast-group>224.2.2.3</multicast-group> | |
<multicast-port>54327</multicast-port> | |
</multicast> | |
</join> | |
</network> | |
<multiMap name="mm"></multiMap> | |
</hazelcast> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mm; | |
import java.util.Random; | |
import java.util.UUID; | |
import com.hazelcast.core.MultiMap; | |
public class Putter implements Runnable { | |
private MultiMap<Integer, UUID> map; | |
private boolean running = true; | |
public Putter(MultiMap<Integer, UUID> map) { | |
this.map = map; | |
} | |
public void setRunning(boolean running) { | |
this.running = running; | |
} | |
public void run() { | |
Random random = new Random(); | |
while(running) { | |
UUID uuid = UUID.randomUUID(); | |
map.put(random.nextInt(10), uuid); | |
try { | |
Thread.sleep(random.nextInt(100)); | |
} catch (InterruptedException e) {} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mm; | |
import java.util.Collection; | |
import java.util.Random; | |
import java.util.UUID; | |
import com.hazelcast.core.MultiMap; | |
public class Remover implements Runnable { | |
private MultiMap<Integer, UUID> map; | |
private boolean running = true; | |
public Remover(MultiMap<Integer, UUID> map) { | |
this.map = map; | |
} | |
public void setRunning(boolean running) { | |
this.running = running; | |
} | |
public void run() { | |
Random random = new Random(); | |
while(running) { | |
for (int i : map.keySet()) { | |
if (map.tryLock(i)) { | |
Collection<UUID> uuids = map.get(i); | |
if (uuids != null) { | |
for (UUID uuid : uuids) | |
map.remove(i, uuid); | |
} | |
try { | |
Thread.sleep(10); | |
} catch (InterruptedException e) {} | |
map.unlock(i); | |
} | |
} | |
try { | |
Thread.sleep(random.nextInt(100)); | |
} catch (InterruptedException e) {} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment