Improving Lock Performance in Java--reference

After we introduced  to  couple of months ago,we have started to receive queries similar to “hey,great,Now I understand what is causing my performance issues,but what I am supposed to do Now?”

We are working hard to build the solution instructions into our own product,but in this post I am going to share several common techniques you can apply independent of the tool used for detecting the lock. The methods include lock splitting,concurrent data structures,protecting the data instead of the code and lock scope reduction.

Locking is not evil,lock contention is

Whenever you face a performance  problem with the threaded code there is a chance that you will start blaming locks. After all,common “kNowledge” is that locks are slow and limit scalability. So if you are equipped with this “kNowledge” and start to optimize the code and getting rid of locks there is a chance that you end up introducing nasty concurrency bugs that will surface later on.

So it is important to understand the difference between contended and uncontended locks. Lock contention occurs when a thread is trying to enter the synchronized block/method currently executed by another thread. This second thread is Now forced to wait until the first thread has completed executing the synchronized block and releases the monitor. When only one thread at a time is trying to execute the synchronized code,the lock stays uncontended.

As a matter of fact,synchronization in JVM is optimized for the uncontended case and for the vast majority of the applications,uncontended locks pose next to no overhead during execution. So,it is not locks you should blame for performance,but contended locks. Equipped with this kNowledge,lets see what we can do to reduce either the likelihood of contention or the length of the contention.

Protect the data not the code

A quick way to achieve thread-safety is to lock access to the whole method. For example,take look at the following example,illustrating a naive attempt to build an online poker server:

Highlighter_490540" class="SyntaxHighlighter ">
nes">
 
 > tables =  >();
   
  table.getLimit()) {
tablePlayers = tables.get(table.getId());
 
   
   
   

The intentions of the author have been good - when new players join() the table,there must be a guarantee that the number of players seated at the table would not exceed the table capacity of nine.

But whenever such a solution would actually be responsible for seating players to tables - even on a poker site with moderate traffic,the system would be doomed to constantly trigger contention events by threads waiting for the lock to be released. Locked block contains account balance and table limit checks which potentially can involve expensive operations both increasing the likelihood and length of the contention.

First step towards solution would be making sure we are protecting the data,not the code by moving the synchronization from the method declaration to the method body. In the minimalistic example above,it might not change much at the first place. But lets consider the whole GameServerinterface,not just the single join() method:

Highlighter_590840" class="SyntaxHighlighter ">
nes">
 
 >();
  
 
  table.getLimit()) {
tablePlayers = tables.get(table.getId());
 
  
  
  

What originally seemed to be a minor change,Now affects the behavIoUr of the whole class. Whenever players were joining tables,the prevIoUsly synchronized methods locked on theGameServer instance (this) and introduced contention events to players trying to simultaneouslyleave() tables. Moving the lock from the method signature to the method body postpones the locking and reduces the contention likelihood.

Reduce the lock scope

Now,after making sure it is the data we actually protect,not the code,we should make sure our solution is locking only what is necessary - for example when the code above is rewritten as follows:

Highlighter_585623" class="SyntaxHighlighter ">
nes">
  
 
  table.getLimit()) {
 
tablePlayers = tables.get(table.getId());
 

then the potentially time-consuming operation of checking player account balance (which potentially can involve IO operations) is Now outside the lock scope. Notice that the lock was introduced only to protect against exceeding the table capacity and the  account balance check is not anyhow part of this protective measure.

Split your locks

When we look at the last code example,you can clearly notice that the whole data structure is protected by the same lock. Considering that we might hold thousands of poker tables in this structure,it still poses a high risk for contention events  as we have to protect each table separately from overflowing in capacity.

For this there is an easy way to introduce individual locks per table,such as in the following example:

Highlighter_145380" class="SyntaxHighlighter ">
nes">
  
 
  table.getLimit()) {
tablePlayers = tables.get(table.getId());
 
 

Now,if we synchronize the access only to the same table instead of all the tables,we have significantly reduced the likelihood of locks becoming contended. Having for example 100 tables in our data structure,the likelihood of the contention is Now 100x smaller than before.

Use concurrent data structures

Another improvement is to drop the Traditional single-threaded data structures and use data structures designed explicitly for concurrent usage. For example,when picking ConcurrentHashMapto store all your poker tables would result in code similar to following:

The synchronization in join() and leave() methods is still behaving as in our previous example,as we need to protect the integrity of individual tables. So no help from ConcurrentHashMap in this regards. But as we are also creating new tables and destroying tables in createTable() and destroyTable()methods,all these operations to the ConcurrentHashMap are fully concurrent,permitting to increase or reduce the number of tables in parallel.

Other tips and tricks

Hope the article helped you to solve the lock contention issues,independent of whether you are using Plumbr or manually extracting the information from thread dumps.

reference from:

http://java.dzone.com/articles/improving-lock-performance

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...