| Author |
beginner's SYNCHRONIZATION question:
|
Nigel Shrin
Ranch Hand
Joined: May 18, 2009
Posts: 119
|
|
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: 819
|
|
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: 119
|
|
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: 16695
|
|
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: 245
|
|
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: 119
|
|
Thank you for your comments Harry and Larry
I'll describe my parens/bracket/brace type better in future!
|
 |
 |
|
|
subject: beginner's SYNCHRONIZATION question:
|
|
|