• 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

Failed to Acquiring Lock using synchronization

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All

I tried to synchronised one block by using synchronized ( this ){ }

i have one scheduler to invoke this block each 2 secs, if it is locked scheduler shouldn't take control on method, but am not acquiring lock on that method.
is there any other way to lock method / block of code.

Note : i tried with synchronized keyword as well to get lock of method, but i failed.

I Really appreciate for your solutions.

Thanks
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show us some of your code?
 
Naresh Talluri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


these are the two ways i used.
 
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

The request for code wasn't for the syntax that you used -- I am pretty sure every one here knows the syntax...

It was a request for code that demonstrates your issue.

Henry
 
Naresh Talluri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code snippet is like below





it will be executed each 5 secs by scheduler ( am using Cron Trigger to execute this metho )
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, first of all you can't get a lock on a method. Objects are what you get locks on. But more to the point, what makes you think you aren't getting a lock?
 
Henry Wong
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

Paul Clapham wrote:Well, first of all you can't get a lock on a method. Objects are what you get locks on. But more to the point, what makes you think you aren't getting a lock?



Agreed. It is more likely that the threads are incorrectly synchronized, or synchronizing on different objects, than there being a bug in the synchronization mechanism that hasn't been found in the last 15 years.

Henry
 
Henry Wong
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

Naresh Talluri wrote:code snippet is like below



Snippets, or any non runnable code, aren't very useful -- as threading issues tend to be race conditions, which needs to be actually ran in order to be found.

Henry
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're having just one synchronized block, on 'contextMessageMap' reference. How does that prevent 2 threads from simultaneously executing the method 'doPersistJob'?
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i tried with synchronized keyword as well to get lock of method, but i failed


By making the method synchronized, you're trying to get a lock on current object (the one for which this method is called), while your other synchronized block is on a map. These locks will not be mutually connected anyway.

Based on the limited information I got from your posts, I think what you need is:



Please be aware, that I've no idea about how your map parameter is shared across threads. So, this may trigger some other synchronization issues. But, at least you'll move a step further.
 
reply
    Bookmark Topic Watch Topic
  • New Topic