File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt on Synchronized block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt on Synchronized block" Watch "Doubt on Synchronized block" New topic

Doubt on Synchronized block

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Hello Ranchers,

Need your help to understand this.

What is difference between line1, line 2 and line 3.
Line 2 is giving me the error. Can you tell me why its?


[BSouther: Added UBB CODE tags]
[ February 21, 2008: Message edited by: Ben Souther ]

Thanks<br />Dinesh
Deepak Chopra
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Can you please paste your code using [code] tag, It is really very difficult to understand the code..!! need more time..!!

all I understood from your code is that:

Line 1: Class level lock --> It is good when we have multiple threads running on different instance.

Line 2: Locking nothing --> compiler Error

Line 3: Object Level Lock --> It is good when we have multiple thread running on same instance.

Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Ian Edwards
Ranch Hand

Joined: Aug 14, 2006
Posts: 107
You have a syntax error at line 2. You are using the synchronized keyword but not telling it which object you want to synchronize on. You want to use something like you have at line 1.
I agree. Here's the link:
subject: Doubt on Synchronized block
It's not a secret anymore!