• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is this call synchronized ?

 
Ajay Xavier
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

consider the following class

class Test
{
public synchronized void mthd1()
{
mthd2();
}

public void mthd2()
{
// stuff something
}
}

Is call to mthd2() synchronized ? because mthd2() gets called only inside mthd1 which is a synchronized method.

Regards,
Ajay.
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronized works at thread level. It could be possible for some other thread to invoke the method2 directly. If you are very sure that method2 is called from outside then you can "assume" it's synchronized but why have it as public if it's not suppose to be called from outside directly.
 
Ajay Xavier
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Purushothaman.

but i am very sure mthd2() gets called only inside mthd1. There is no other calls to this method (or as private according to your reply) is it synchronized now?
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can consider method2 as well to be atomic provided there is no other unsynchronized method within the same class which calls method2.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is little point in not making the second method synchronized. That would help guard against unwary programmers accessing it from within that class without synchronization and would convey the idea that it must be synchronized to future developers.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Blair:
There is little point in not making the second method synchronized. That would help guard against unwary programmers accessing it from within that class without synchronization and would convey the idea that it must be synchronized to future developers.


Thats why I like to add


But generally I wouldn't expose that method like that. I guess you choose between deadlock or invalid state possibly.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where would the deadlock be?
 
Ajay Xavier
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken thanks for your reply.

There is little point in not making the second method synchronized. That would help guard against unwary programmers accessing it from within that class without synchronization and would convey the idea that it must be synchronized to future developers.


Do you suggest mthd2() should be synchronized so that future developers would be aware that mthd2() should be called in a synchronized context?
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ajay Xavier:
Ken thanks for your reply.



Do you suggest mthd2() should be synchronized so that future developers would be aware that mthd2() should be called in a synchronized context?


Of course. its public. it can never be called without synchronization. So why should it not be synchronized? Otherwise make it private and add the assert.
 
Ajay Xavier
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course. its public. it can never be called without synchronization. So why should it not be synchronized? Otherwise make it private and add the assert.



we can make mthd2() as private. but what the point in adding the assert staement to it.

Regards,
Ajay.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to be missing the point. If method2 requires synchronization then make it synchronized, if it doesn't then don't. Playing this game of trying to make it not synchronized while only calling it from synchronized contexts because it needs to be synchronized is silly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic