multithreading - Understanding and confusion over synchronization in java -


suppose have

class {     synchronized void m1() {     }     synchronized void m2() {     }     void m3() {     }     static void m6() {         synchronized(this){         }     } } 

and 2 instances of class a1&a2.

now if thread t1 instance a1 call method m1,there restriction thread t2 instance a1 can't execute method m1 untill t1 finishes execution of m1. , t2 instance a2 can execute m1().

now doubt can thread t1 a1 can execute other method's(except m1) parallaly while executing m1?

what's difference b/w synchronized block , synchronized method? difference block have lesser scope synchronization efficient?

i read article stating "synchronized block can throw java.lang.nullpointerexception if expression provided block parameter evaluates null".

we use "this" parameter synchronized block, there , case using synchronized block inside static method.beacuse don't need object instantian execute static method.

where did misunderstood it?

now if thread t1 instance a1 call method m1,there restriction thread t2 instance a1 can't execute method m1 untill t1 finishes execution of m1. , t2 instance a2 can execute m1().

this correct. since both threads synchronized on same instance, first thread gets execute , second thread gets wait.

now doubt can thread t1 a1 can execute other method's(except m1) parallaly while executing m1?

yes, known reentrant synchronization. take @ end of tutorial.

what's difference b/w synchronized block , synchronized method? difference block have lesser scope synchronization efficient?

a synchronized block can used on instance. synchronized method synchronizes on this implicitly. try use synchronized blocks possible around actual critical section.

i raed article stating "synchronized block can throw java.lang.nullpointerexception if expression provided block parameter evaluates null".

the following

synchronized(null) {...} 

would throw nullpointerexception.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -