Reputation: 79
I have a map like this as shown below...
Key Value
23 20
32 20 (20+20 =40 , min=23 max=32)
43 18
45 24 (24+18 =42 , since 42 >40 so here min and max will be same that is 43
47 10
56 6 (24 +10 +6 =40) so here min =45 and max = 56
49 2
47 12
so as shown above there will be final constant named split whose value is 40
final int SPLIT = 40; //this will be configurable as it value can be changed.
so I have to implmenet the logic such as that if the value of the map reaches to 40 then first key of the map from where the calculation started and he key where exactly it reaches to 40wilkl be choosen as min and max also explained above
besides this care need to be taken the\at if sum reaches more than 40 then we have to ignore it and taking the previous value itself as min amd max in tha case min and max will be equal.
Please advise how to achieve the same through java in map
Upvotes: 1
Views: 2083
Reputation: 4137
There are several frameworks that can take that for you,
In general you need to work with a shared data structure across the JVMs (i.e - clustering).
I would recommend for example to check one of the following frameworks:
A. Infinispan - which provides you a shared data grid, but also has caching (i.e - eviction policies for example) strategies
B. EHCache - distributed cache
I would recommend you to make sure your application is loosely coupled from the cache/data-grid implementation - for example use this approach:
Map<String,Integer> sharedJvmMap = SharedMapBuilderFactory.instance().getBuilder().createSharedMap();
And have a builder hierarchy that creates for you the shared map.
A builder can look like:
public interface SharedMapBuilder {
<K,V> Map<K,V> createSharedMap();
}
Have for example an infinispan implementation for this class, and have some configuration file defining you use this builder.
Another option is just to inject the proper builder (using Spring, Juice, or any DI container).
This way, your application code will not be aware of the internal implementation of the shared map.
Upvotes: 0
Reputation: 132
You'll have to rely on techniques used to synchronize/communicate between different processes, not different JVM threads. Take a look at this thread: Any concept of shared memory in Java
Upvotes: 0
Reputation: 533462
I would redesign your application so it doesn't need a global lock. Implement your system so only one JVM need the lock, and every other JVM talks to it do what is needed.
There are many alternatives, using ServerSocket, file locks, data locks, but IMHO these are much more difficult to get right or efficient.
Upvotes: 3