Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

beginner's SYNCHRONIZATION question:

 
Nigel Shrin
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Nigel Shrin
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your comments Harry and Larry
I'll describe my parens/bracket/brace type better in future!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic