Created
August 3, 2012 08:30
-
-
Save psfung/3245858 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.Calendar; | |
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 { | |
//args[0] = start minute | |
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); | |
Calendar start = Calendar.getInstance(); | |
start.set(Calendar.MINUTE, Integer.parseInt(args[0])); | |
start.set(Calendar.SECOND, 0); | |
start.set(Calendar.MILLISECOND, 0); | |
try { | |
Thread.sleep(start.getTimeInMillis() - System.currentTimeMillis()); | |
} catch (InterruptedException e) {} | |
System.out.println("start!"); | |
putterThread.start(); | |
removerThread.start(); | |
try { | |
Thread.sleep(100000); | |
} 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 int successCount; | |
private boolean running = true; | |
public Putter(MultiMap<Integer, UUID> map) { | |
this.map = map; | |
} | |
public int getSuccessCount() { | |
return successCount; | |
} | |
public void setRunning(boolean running) { | |
this.running = running; | |
} | |
public void run() { | |
Random random = new Random(); | |
while(running) { | |
UUID uuid = UUID.randomUUID(); | |
map.lock(1); | |
map.put(1, uuid); | |
System.out.println(successCount++); | |
map.unlock(1); | |
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) { | |
if (map.tryLock(1)) { | |
Collection<UUID> ids= map.get(1); | |
if (ids != null) | |
for (UUID id : ids) | |
map.remove(1, id); | |
map.unlock(1); | |
} | |
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