官术网_书友最值得收藏!

How to do it...

This recipe will take you through three scenarios. Let's get started:

  1. In the first scenario, LockType is defined at the class level:
@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
@Lock(LockType.READ)
@AccessTimeout(value = 10000)
public class UserClassLevelBean {

private int userCount;

public int getUserCount() {
return userCount;
}

public void addUser(){
userCount++;
}

}
  1. In the second scenario, LockType is defined at the method level:
@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
@AccessTimeout(value = 10000)
public class UserMethodLevelBean {

private int userCount;

@Lock(LockType.READ)
public int getUserCount(){
return userCount;
}

@Lock(LockType.WRITE)
public void addUser(){
userCount++;
}
}
  1. The third scenario is a self-managed bean:
@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class UserSelfManagedBean {

private int userCount;

public int getUserCount() {
return userCount;
}

public synchronized void addUser(){
userCount++;
}
}

Now, let's see how this recipe works.

主站蜘蛛池模板: 涟源市| 佳木斯市| 桂阳县| 浦北县| 丹江口市| 湘阴县| 舒城县| 穆棱市| 连州市| 白银市| 博爱县| SHOW| 德保县| 武功县| 滨州市| 河源市| 分宜县| 新绛县| 东平县| 和平县| 荆州市| 班玛县| 突泉县| 和林格尔县| 北海市| 登封市| 方城县| 九江县| 阿巴嘎旗| 建平县| 汤原县| 宣武区| 浮山县| 景宁| 繁峙县| 阿图什市| 合作市| 区。| 安乡县| 林甸县| 玉门市|