Grails Service 并发处理(笔记)

grails Service是单例的,所以容易出并发问题,当出现并发时怎么处理?在调用的方法名称上面增加@Synchronized。


这个是groovy多线程的处理,非常简单就能解决异步问题。


groovy的异步用法有很多种,以下是groovy帮助文档里面的原文。

-------------------------------------------------------------


Method annotation to make a method call synchronized for concurrency handling with some useful baked-in conventions.


@Synchronized is a safer variant of the synchronized method modifier. The annotation can only be used on static and instance methods. It operates similarly to the synchronized keyword, but it locks on different objects. When used with an instance method, the synchronized keyword locks on this, but the annotation locks on a (by default automatically generated) field named $lock. If the field does not exist, it is created for you. If you annotate a static method, the annotation locks on a static field named $LOCK instead.


If you want, you can create these locks yourself. The $lock and $LOCK fields will not be generated if you create them yourself. You can also choose to lock on another field, by specifying its name as parameter to the @Synchronized annotation. In this usage variant, the lock field will not be created automatically, and you must explicitly create it yourself.


Rationale: Locking on this or your own class object can have unfortunate side-effects, as other code not under your control can lock on these objects as well, which can cause race conditions and other nasty threading-related bugs.


Example usage:


 

class SynchronizedExample {
   private final myLock = new Object()


   @Synchronized
   static void greet() {
     println "world"
   }


   @Synchronized
   int answerToEverything() {
     return 42
   }


   @Synchronized("myLock")
   void foo() {
     println "bar"
   }
 }


 
which becomes:
 
class SynchronizedExample {
   private static final $LOCK = new Object[0]
   private final $lock = new Object[0]
   private final myLock = new Object()


   static void greet() {
     synchronized($LOCK) {
       println "world"
     }
   }


   int answerToEverything() {
     synchronized($lock) {
       return 42
     }
   }


   void foo() {
     synchronized(myLock) {
       println "bar"
     }
   }
 }


 
Credits: this annotation is inspired by the Project Lombok annotation of the same name. The functionality has been kept similar to ease the learning curve when swapping between these two tools.
Details: If $lock and/or $LOCK are auto-generated, the fields are initialized with an empty Object[] array, and not just a new Object() as many snippets using this pattern tend to use. This is because a new Object is NOT serializable, but a 0-size array is. Therefore, using @Synchronized will not prevent your object from being serialized.


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值