File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes beginner's SYNCHRONIZATION question: 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 "beginner Watch "beginner New topic
Author

beginner's SYNCHRONIZATION question:

Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 137
when synchronizing a block only, I am correct in saying you can use it with or without brackets() - what goes in the brackets?



or



thank you!!


Nigel
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Nigel Shrin wrote:when synchronizing a block only, I am correct in saying you can use it with or without brackets() - what goes in the brackets?



or



thank you!!


so, try some code that has synchronize with and without a target. What happens?
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Nigel Shrin wrote:when synchronizing a block only, I am correct in saying you can use it with or without brackets() - what goes in the brackets?

In () brackets you specify an object on which a lock is acquired.
You cannot use synchronize keyword without () brackets because you will get compilation error: "'(' expected ....... synchronized{ ".

Look here for explanation of synchronized blocks: http://java.sun.com/docs/books/tutorial/essential/concurrency/locksync.html
Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 137
Hi Tim & Ireneusz - thanks for replies - no target no compilation, and it thinks the opening brace is the beginning of an enum definition:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error, insert "enum Identifier" to complete EnumHeaderName
Syntax error, insert "EnumBody" to complete BlockStatements

Thanks!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Nigel Shrin wrote:Hi Tim & Ireneusz - thanks for replies - no target no compilation, and it thinks the opening brace is the beginning of an enum definition:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error, insert "enum Identifier" to complete EnumHeaderName
Syntax error, insert "EnumBody" to complete BlockStatements


Please stop running programs that have compile problems. Get into the habit of fixing *all* compile errors before running anything.


Also... When showing compile errors, show us the compile error. Not the runtime error that reports the compile error.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Nigel Shrin wrote:when synchronizing a block only, I am correct in saying you can use it with or without brackets() - what goes in the brackets? ...


This is a bit off-topic but I thought I should mention this because we all come from different regions of the English-speaking world that use words differently. In the United States, these are the usual specific names for the various common kinds of brackets:

( ) = parentheses, parens,
[ ] = square brackets, box brackets
{ }= braces, curly brackets
<>= angle brackets

Since Java is so strict about syntax, we need to be strict with our terms when speaking with fellow programmers.


SCJP 6
Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 137
Thank you for your comments Harry and Larry
I'll describe my parens/bracket/brace type better in future!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: beginner's SYNCHRONIZATION question:
 
Similar Threads
lock on a method?
forward, include and sendRequest
object lock
problem in EL
Traversing the table using arrow keys