Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Failed to Acquiring Lock using synchronization  RSS feed

 
Naresh Talluri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • 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
 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator


these are the two ways i used.
 
Henry Wong
author
Sheriff
Posts: 22842
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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 )
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 22842
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 22842
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!