`

7. multi-thread: deadlock

 
阅读更多
1. What is deadlock ?
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. how to detect deadlock ?
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

3. Why we call start() method which in turns calls run() method, why not we directly call run() method ?

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

4. Why do we need Synchronization in Java?

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.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics