Hi,
I have a class that contains both static and non-static methods, abstracted as such:
I need to synchronize these two methods - i.e. if a thread attempts to run one while another thread is running the other, *bad* things can happen...
Now, I know that if i synchronize both of these methods (i.e. write the "synchronized" keyword as part of their signature) they will be synchronized, but on different locks... the static method on the associated Class object, and the non-static instance method in the instance object. Does this mean that a separate thread can still run each of them simultaneously? If so, my issue is not resolved. How can I neatly synchronize them so that, if a thread attempts to execute the static method (for example) and another is running the instance method, the static-method execution will wait until the instance-method has completed, and vice-versa?
Thanks....
dora
I have a class that contains both static and non-static methods, abstracted as such:
Code:
class MyClass
{
static void doThis()
{
//-- code here...
};
public void doThat()
{
//-- code here...
};
};
I need to synchronize these two methods - i.e. if a thread attempts to run one while another thread is running the other, *bad* things can happen...
Now, I know that if i synchronize both of these methods (i.e. write the "synchronized" keyword as part of their signature) they will be synchronized, but on different locks... the static method on the associated Class object, and the non-static instance method in the instance object. Does this mean that a separate thread can still run each of them simultaneously? If so, my issue is not resolved. How can I neatly synchronize them so that, if a thread attempts to execute the static method (for example) and another is running the instance method, the static-method execution will wait until the instance-method has completed, and vice-versa?
Thanks....
dora