Sunday, March 8, 2015

Java Threads key points to Remember

1) Thread do not come out of a waiting pool just because a lock has been released.

2) Thread must own the lock on the object that wait() is being invoked on. 

3) notifyAll() notifies all the threads waiting on a particular locked object,
    not all threads waiting on any object.

4) when synchronized instance methods are called on the same instance, they block each 
    other.   

5) synchronized static methods in the same class block each other, regardless of which      
    instance was used  to call the methods.

6) synchronized instance methods called on different instances do not block each other

7) The join() must be placed in a try/catch block.

8) wait(), notify(), notifyAll() must be call from within the synchronized area

9) A thread is done being a thread when its target run() method completes.

10) A thread can call the start() method only once in its lifetime. If start() is called
      more than once on a Thread object, it will throw a RuntimeException

11) sleep(), join() and wait() method throws InterruptedException

12) join(), setPriority(), wait(), notify() and notifyAll() are final method.

13) sleep() and yield() are static methods.

14) thread states are new, runnable, running, waiting/block/sleeping, dead

15) yield() method is supposed to do is make the currently running thread back to runnable 
      to allow other threads of  the same priority to get their turn.

16) A yield() won't ever cause a thread to go to the waiting/sleeping/ blocking state.

17) If possible then the synchronized code should be kept minimum.

18) A static synchronized method and a non-static synchronized method will not block each 
      other, ever.

19) Threads calling static synchronized methods in the same class will always block each 
      other—they all lock on the same Class instance.

20) Threads calling non-static synchronized methods in the same class will only block each 
      other if they're invoked using the same instance.

No comments:

Post a Comment

Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...