I am thread newbie and am currently going through the thread synchronization chapter. I have a question regarding one scenario in thread locking
Here is what I know:
1: When I put a instance lock in a instance method(i.e.)
private Object lock1= new Object();
private Object lock2 = new Object();
void f1(){
synchronized (lock1) {
}
}
void f2(){
synchronized (lock2 ) {
}
}
void f4(){
synchronized (lock2 ) {
}
}
void f3(){
synchronized (lock1) {
}
}
Here what will happen is that, if there is object A of a class X being shared by multiple threads, and some thread t1 is executing f1's block, then until t1 comes out f1 block, all the other threads trying to enter function f3,,f1 will be blocked. Same is the case with f2 and f4.
Now in case of static locks, if a class has multiple static methods, and we want individual locking for those methods rather than class locking, we will have multiple static locks. And those locks will determine, what methods will be blocked.
Up until this point everything is fine. Now If put this static locks in a instance method, what will happen, when two thread on the same object try to access that method?