aspose file tools
The moose likes Threads and Synchronization and the fly likes Notification of synchronized method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Notification of synchronized method" Watch "Notification of synchronized method" New topic
Author

Notification of synchronized method

Rajeev Kashyap
Greenhorn

Joined: Sep 07, 2004
Posts: 19
How my calling method will know that method is synchronized and has acquired monitor state.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Rajeev Kashyap
Greenhorn

Joined: Sep 07, 2004
Posts: 19
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

Joined: Jul 11, 2001
Posts: 14112
Why isn't delete simply synchronized, too???


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rajeev Kashyap
Greenhorn

Joined: Sep 07, 2004
Posts: 19
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
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

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.


[Jess in Action][AskingGoodQuestions]
Rajeev Kashyap
Greenhorn

Joined: Sep 07, 2004
Posts: 19
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
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Notification of synchronized method
 
Similar Threads
Threads
Abstract method
Lock and Unlock methods
Can Overloaded method be synchronized
Threads - synchronized methods