• Post Reply Bookmark Topic Watch Topic
  • New Topic

Notification of synchronized method  RSS feed

 
Rajeev Kashyap
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How my calling method will know that method is synchronized and has acquired monitor state.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure that you will. If you call a method that happens to be synchronized your thread will block - sit there doing nothing but waiting - until it can get the lock. Your calling method might observe that the call is taking a long time but I think that's about it.

Do you have a requirement to know more than that? Like keep count of the number of waiting threads or wait time statistics? You could do some kind of command queuing to get that stuff. Lemme know if that sounds interesting.
 
Rajeev Kashyap
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx for reply.
My actual problem is I have two function one update and another delete for my DB Management. Update is synchronized but delete is not. So if my delete operation is called it has to first check out whether any thread is updating or not. If update has acquired the monitor state then it will wait to come out but if not then it will perform delete operation. That is my requirement.

Thanks in adv
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why isn't delete simply synchronized, too???
 
Rajeev Kashyap
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By synchronizing delete method it does not fulfill my purpose. Though it is synchronized or not while calling update it should check out for the monitor state of update method. So, I don' t think by synchronizing update method it will solve my purpose.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajeev --

If update and delete are both synchronized using the same object, they will share a single monitor, and therefore only one will be able to run at a time. This is, in fact, the whole point of synchronization -- that's exactly what it's supposed to be used for.
 
Rajeev Kashyap
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest,
But suppose two users have logged on. So two threads will be started at a time. So if one thread has acquired monitor state and other thread is trying to delete then how should I come across this problem
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure whether I'm not understanding you, or you're not understanding me.

Imagine there's a single object in your system which represents the database (note that I'm not saying this is necessary or even good; I'm just trusting that you've already somehow determined this is the case.) That single object has synchronized update() and delete() methods. ANy number of client threads are all trying to use that object. As long as any one thread is inside either update() or delete(), no other thread can use either of those methods; they'll just automatically and transparently wait for the first thread to be through. Again, that's just how synchronization works.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!