A Neat Little REST Trick

So there is a shared memory problem. We are using the REST template and a few solutions come to mind. Perhaps it is time to not rule out statics.

The Controller component is established as a Singleton whose methods are called by every connection. It is possible to include a static synchronized static object to avoid. Java’s concurrent classes have a few of these. Synchronized classes and maps of locks are way more complicated and likely to slow things down. Synchronized methods in fact lock all Class level methods and attributes (e.g. synchronize(this)).

As a warning though, the CopyOnWriteArrayList is thread safe but slow for writing to.

The example below is of a ConcurrentHashMap<String,Integer> but the types are sometimes not showing, sorry.

@RestController
public class RESTClass{
    private static ConcurrentHashMap<String,Integer> mp = new ConcurrentHashMap<String,Ingeter>(); 
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s