// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;

public class Test1 {
    public static class Counter implements Serializable {
        private static final long serialVersionUID = 1L;
        private int counter = 0;
        public int getCounter() {
            return counter;
        }
        public void increment() {
            counter++;
        }
    }
    private static final int N = 10000;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.setProperty("hazelcast.logging.type", "none");
        HazelcastInstance hci = Hazelcast.newHazelcastInstance();
        IMap<Object, Object> cache = hci.getMap("MyCache");
        cache.put("counter", new Counter());
        System.out.print("Press enter to start");
        br.readLine();
        for(int i = 0; i < N; i++) {
            cache.lock("counter");
            try {
                Counter c = (Counter)cache.get("counter");
                c.increment();
                cache.put("counter", c);
            } finally {
                cache.unlock("counter");
            }
            if((i % 100) == 0) System.out.println(i);
        }
        Counter c = (Counter)cache.get("counter");
        System.out.println("Counter=" + c.getCounter());
        System.out.print("Press enter to stop");
        br.readLine();
        cache.destroy();
        hci.shutdown();
    }
}
