Hi Qiming, for this particular example, you could use an AtomicInteger to hold the counter value, e.g.
import x10.util.concurrent.AtomicInteger; ... val o = new AtomicInteger(0); var t:Long = -System.nanoTime(); for (var i:int=0; i < 10000; i++) { o.getAndIncrement(); } on my computer this takes only 45% of the time of the syncIncr() method. Some problems may also permit the use of weak atomicity, in which case var t1:Long = -System.nanoTime(); for (var i:int=0; i < 10000; i++) { o.weakCompareAndSet(i, i+1); } is faster still (about 35% of the time for syncIncr()). However, your subject header suggests that you want an efficient implementation of an object lock, as per Java synchronized. I don't think there is a plan for X10 to support such a lock, and your implementation (adding a Lock object inside the class) is probably as good as any. By the way, what performance do you expect from a synchronized method? On my system, a Java synchronized method achieves roughly the same performance as your syncIncr() method. The "correct" X10 way to synchronize is using atomic blocks. However these are currently inefficient for most purposes as they translate to a place-level lock. There are near-term plans to implement efficient single-field atomic updates - see http://jira.codehaus.org/browse/XTENLANG-321. In the longer term, an efficient implementation would use software transactional memory. Cheers, Josh On 10/01/11 21:19, Qi Ming Teng wrote: > Hi, > > I want to simulate the Java 'synchronized' methods in X10, so what I > did is to add a lock to the object. As shown in the attached source code > below, the 'syncIncr()' method is expected to yield a good performance. > The problem is that I am not getting a good result here: > > Profile result: > > t(b) =38273 // 38 us > t(s) =693384 // 693 us > > Any suggestions to improve the program? > > Source code: > > 1 public class TestLock { > 2 public static class Counter { > 3 private var c:int; > 4 private val lock:Lock; > 5 public def this() { > 6 c = 0; > 7 lock = new Lock(); > 8 } > 9 > 10 public def incr() { > 11 this.c = this.c + 1; > 12 } > 13 > 14 public def syncIncr() { > 15 lock.lock(); > 16 this.c = this.c + 1; > 17 lock.unlock(); > 18 } > 19 } > 20 > 21 public static def main(Array[String](1)) { > 22 val o = new Counter(); > 23 > 24 var t:Long = -System.nanoTime(); > 25 for (var i:int=0; i< 10000; i++) { > 26 o.incr(); > 27 } > 28 > 29 t += System.nanoTime(); > 30 Console.ERR.println(" t(b) =" + t); > 31 > 32 var t1:Long = -System.nanoTime(); > 33 for (var i:int=0; i< 10000; i++) { > 34 o.syncIncr(); > 35 } > 36 t1 += System.nanoTime(); > 37 Console.ERR.println(" t(s) =" + t1); > 38 } > 39 } > > Regards, > - Qiming > ------------------------------------------------------------------------------ > Gaining the trust of online customers is vital for the success of any company > that requires sensitive data to be transmitted over the Web. Learn how to > best implement a security strategy that keeps consumers' information secure > and instills the confidence they need to proceed with transactions. > http://p.sf.net/sfu/oracle-sfdevnl > _______________________________________________ > X10-users mailing list > X10-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/x10-users > ------------------------------------------------------------------------------ Gaining the trust of online customers is vital for the success of any company that requires sensitive data to be transmitted over the Web. Learn how to best implement a security strategy that keeps consumers' information secure and instills the confidence they need to proceed with transactions. http://p.sf.net/sfu/oracle-sfdevnl _______________________________________________ X10-users mailing list X10-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/x10-users