I was going through multi-Threading concepts.
public class Foo{
public void testMethod1(){
synchronized(foo.class){
// only one thread can access this block at a time
}
}
// or i can use the below method
public void testMethod2(){
synchronized(SomeClass.class){
// only one thread can access this block at a time
}
}
}
I will use either testMethod1 or testMethod2 in my code.
As you can see i am using synchronized
on the Foo.class in testMethod1()
,
and SomeClass.class in testMethod2()
.
If anyone method i am using it is giving same result in multiple thread access. I want to know the difference between the usages, when I have to use same class for synchronized block and some other class for synchronized block.
Or there is any difference between the above two methods?