9
If I have a class, where I have two methods one static and the other not.
Is the lock the same or not? How to do for the two sharing methods, the same synchronization mechanism, ie the same lock?
class Foo{
public synchronized static void test(){}
public synchronized void test2(){}
}
This would be a Thread-Safe class?
Cool @Maiconcarraro, interesting mechanism. And how would it be if I wanted the lock of the static method to be the same as the non-static method, in other words, how to make both types of method share the mesom synchronization mechanism?
– Filipe Miranda
You mean the
test()
function astest2()
? I really can’t say, I know the other way around would be possible, but maybe you wanted to play with Reentrantlock– Maicon Carraro
If the method is static, lock is done in the class
Foo.class
. So just make the dynamic methods have lock in the class as wellFoo.class
(this is done by placing the method code inside a blocksynchronized(Foo.class) { ... }
instead of declaring the method itself assynchronized
).– Piovezan
@Piovezan, cool, great solution, really works!
– Filipe Miranda