This is a version of the example shown in the quick start guide, with some additional notes
@Entity // 1 public class Counter { protected int count = 0; public synchronised void increment() { count++; } public synchronised int getCount() { return count; } } public class Demo { public static void main() { Session.setActiveStore(new RemoteStore("127.0.0.1",10987)); // 2 Counter counter = (Counter) store.getRoot("count"); // 3 if(counter = null) { counter = new Counter(); // 4 counter = store.setRootIfNull("count",counter); // 5 } // 6 for(int i=0;i<1000;i++) counter.increment(); // 7 System.out.println("Counter = " + counter.get(); store.shutdown(); // 8 } }
Counter is by its nature thread-safe. Using JEMM it is thread-safe across threads in multiple JVMs.