• Post Reply Bookmark Topic Watch Topic
  • New Topic

Where to put sychronized statement in code?  RSS feed

 
Kari Smith
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where in my code below would I enter the "synchronized" statement? before the ConnectionWrapper or after? My goal is to lock during a data load to prevent duplicates from being created. I intend to put a check constraint at the table level as well.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
synnchronized { ... } goes around the block of code that is the critical section. That is, around the chunk that must execute atomically, with no other thread executing that block or any other block synced on the same lock until it's complete. (Or until you explicitly release the lock with a wait() call.)

Also, please UseCodeTags(⇐link) to make your code readable.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you would only synchronize a block of code to prevent two or more threads in your application from executing that block at the same time. Does your application actually run several threads which might attempt to run that code simultaneously?
 
Kari Smith
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Paul it runs multiple threads.. But good to know to only use synchronized in this case.

Thanks for your feedback Jeff.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!