• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

beginner's SYNCHRONIZATION question:

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your comments Harry and Larry
I'll describe my parens/bracket/brace type better in future!
 
Whatever you say buddy! And I believe this tiny ad too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic