• Post Reply Bookmark Topic Watch Topic
  • New Topic

Snippet in a method surrounded by Synchronized (this) showing no effect  RSS feed

 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I've a method, which multiple threads execute that method. In that method I've small snippet surrounded by synchronized block like below,



"this" means, current thread.
But, all threads are executing this snippet simultaneously. I don't want that. I want to execute that snippet by one thread at a time. Is there any wrong in this code?

Thanks:
Ramakrishna K.C
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramakrishna Udupa wrote:But, all threads are executing this snippet simultaneously.

What makes you think that? Because I'm pretty darn sure that they're not.

Is there any wrong in this code?

Well, I can't see anything wrong with what you've written, but you've left the most important part (the "snippet") out.

Winston
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What makes you think that?

when I put debug point in synchronized snippet, all threads entered into that snippet and stopped at the debug point.

but you've left the most important part (the "snippet") out.


Should I post my original code?
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
below is my original code. I marked synchronized block.. line number 39 to 54.

 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramakrishna Udupa wrote:Hi All,

I've a method, which multiple threads execute that method.

Are all the threads executing on the same instance of the class or have you got a separate instance for each class ?

Ramakrishna Udupa wrote:"this" means, current thread.

If it does then that's your problem. If you're synchronising on the actual Thread instance then every thread will be locking on itself so there will be no synchronization.
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are all the threads executing on the same instance of the class or have you got a separate instance for each class ?


Same instance.

If you're synchronising on the actual Thread instance then every thread will be locking on itself so there will be no synchronization.


I think this is happening. Because, when I write a sample program like below, it works fine.



See the attached snapshot. It works fine. all threads are running and only one thread is in synchronized snippet. But, in above case, its not working. I'm sure, same instance not a different instances.

Thanks:
Ramakrishna K.C
Capture.PNG
[Thumbnail for Capture.PNG]
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramakrishna Udupa wrote:See the attached snapshot. It works fine. all threads are running and only one thread is in synchronized snippet. But, in above case, its not working. I'm sure, same instance not a different instances.

But you said in your OP that 'this' refers to a Thread (which I missed), which is not how synchronization is supposed to happen.

What you do is use it on objects (or methods) that are shared (or run) by many Threads, not on the Threads themselves.

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