Multithreading and Locking Strategies

Hello,
Concurrency in Java 5 has introduced new ways of developing multi-threaded applications.

The basic difference in these two APIs is Locking Strategy that is implemented for achieving the concurrency.

Java 5 concurrency API use Compare And Swap(CAS) mechanism. In CAS the original value is compared with the value that was read last time, if it matches then the thread modifies the shared variable with new value. The thread in this case is optimistic hoping that no other thread has modified the original value. This is the Optimistic locking strategy.

In Java 4, we use the synchronized keyword for locking the resource. This way we use the Pessimistic locking strategy. Because we lock the shared resource exclusively for the current thread. No other thread can access and modify the variable.

Both approaches have their own advantages and shortcomings. In this entry I just wanted to highlight the implementation difference that appealed me while reading the article.

Cheers !!
Amit

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

%d bloggers like this: