Java has no function level lock?
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   George_Simple
Posted On:   Wednesday, March 19, 2003 03:53 AM

Hi, everyone! I am a C++ programmer before and after reading the internal implemention method of Java Thread. I think Java has no function level lock and only object level lock. I mean to acquire a function lock is the same as acquire an object's lock. For example, when objectA.synchronizedFunctionA is called, suppose synchronizedFunctionA is a synchronized function of class A. Java only check whether objectA is locked. The synchronized function synchronizedFunctionA has no individual function level lock. Am I correct? If I am correct, I think in some certain application, function level lock will be more efficient than object level lock. Even though it shou   More>>
			
Hi, everyone!


I am a C++ programmer before and after reading the
internal implemention method of Java Thread. I think
Java has no function level lock and only object level
lock. I mean to acquire a function lock is the same as
acquire an object's lock.

For example, when objectA.synchronizedFunctionA is called,
suppose synchronizedFunctionA is a synchronized function of
class A. Java only check whether objectA is locked. The synchronized function synchronizedFunctionA has no individual function level lock.

Am I correct?

If I am correct, I think in some certain application, function level lock will be more efficient than object level lock. Even though it should be used more carefully.

Thanks in advance,
George
   <<Less

Re: Java has no function level lock?

Posted By:   Marian_Olteanu  
Posted On:   Saturday, March 29, 2003 01:53 AM

If I understood right, you want to have synchronizedFunctionA non-static and you want that only one thread runs code in this function once.
Do this:


public class A
{
private static final Object syncObjectA = new Object();
public void synchronizedFunctionA( .... )
{
synchronized( syncObjectA )
{
// Your sync code
}
}
// Other stuff.......
}


So, you don't have to use singleton pattern or something like that. It is enough to allocate a static object for that method and use it for synchronization.

Re: Java has no function level lock?

Posted By:   Anonymous  
Posted On:   Wednesday, March 19, 2003 10:44 PM

There is one more way I can think of. Make the member function a synchronized static member function.

Re: Java has no function level lock?

Posted By:   Anonymous  
Posted On:   Wednesday, March 19, 2003 10:39 PM

You can make a class singleton and make the required methods
as synchronized methods. This will give you the so called
'function level lock'. I hope this helps you.
About | Sitemap | Contact