Java 5 introduce the lock method. Any pros and cons using lock compared to synchronized keyword?
Asked
Active
Viewed 3,474 times
3 Answers
5
'synchronize' will lock any resources accessed within the method. 'lock' allows you more granularity, and control (e.g. only locking some resources, or locking only if a certain condition is met, for example).
There's a pretty good code sample near the top of this link: http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/locks/Lock.html

jefflunt
- 33,527
- 7
- 88
- 126
1
The java.util.concurrent
locks give you more control on what and when to lock. You can still use the synchronized
keyword if it fits your needs (e.g. if you need per-instance / per-class synchronization)

Bozho
- 588,226
- 146
- 1,060
- 1,140
-1
See the difference between synchronized keyword and lock

Rakesh Soni
- 10,135
- 5
- 44
- 51
-
1Please mention the source of this screenshot or scan. – try-catch-finally Aug 12 '17 at 06:58