1. [b]What is deadlock[/b] ?
when two or more threads waiting for each other to release lock and get stuck for infinite time , situation is called deadlock . it will only happen in case of multitasking.
2. [b]how to detect deadlock[/b] ?
simplest way :
1). under linux: kill -3
this will print status of all the thread in application log file and you can see which thread is locked on which object
2). via jconsole:
jconsole will show you exactly which threads are get locked and on which object
[b]3. Why we call start() method which in turns calls run() method, why not we directly call run() method ?[/b]
when you call start() method it creates new Thread and execute code declared in run() while directly calling run() method doesn’t create any new thread and execute code on same calling thread
[b]4. Why do we need Synchronization in Java?[/b]
1) synchronized keyword in java provides locking which ensures mutual exclusive access of shared resource and prevent data race.
2) synchronized keyword also prevent reordering of code statement by compiler which can cause subtle concurrent issue if we don't use synchronized or volatile keyword.
3) synchronized keyword involve locking and unlocking. before entering into synchronized method or block thread needs to acquire the lock at this point it reads data from main memory than cache and when it release the lock it flushes write operation into main memory which eliminates memory inconsistency errors.
when two or more threads waiting for each other to release lock and get stuck for infinite time , situation is called deadlock . it will only happen in case of multitasking.
2. [b]how to detect deadlock[/b] ?
simplest way :
1). under linux: kill -3
this will print status of all the thread in application log file and you can see which thread is locked on which object
2). via jconsole:
jconsole will show you exactly which threads are get locked and on which object
[b]3. Why we call start() method which in turns calls run() method, why not we directly call run() method ?[/b]
when you call start() method it creates new Thread and execute code declared in run() while directly calling run() method doesn’t create any new thread and execute code on same calling thread
[b]4. Why do we need Synchronization in Java?[/b]
1) synchronized keyword in java provides locking which ensures mutual exclusive access of shared resource and prevent data race.
2) synchronized keyword also prevent reordering of code statement by compiler which can cause subtle concurrent issue if we don't use synchronized or volatile keyword.
3) synchronized keyword involve locking and unlocking. before entering into synchronized method or block thread needs to acquire the lock at this point it reads data from main memory than cache and when it release the lock it flushes write operation into main memory which eliminates memory inconsistency errors.