Showing posts with label setPriority. Show all posts
Showing posts with label setPriority. Show all posts

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.

Tuesday, March 3, 2015

About wait() notify() notifyAll() sleep() yield() setPriority()

wait() method call puts the current running thread into the waiting list/pool of the OBJECT it is working. the threads waiting list/pool is separate for each OBJECT(not thread object but the object/instance it is working for) . the waiting thread release the of the object it is working on.
and the thread calling the wait() method must hold lock on the object(Intance of any class).
the waiting thread will be back into runnable state when any another thread working on the same object(Intance of any class) calls notify()/notifyAll() method.
wait() method must be call from the synchronized context.

notify() method call brings only one thread back into runnable state from the waiting state and that is not gauranteed which thread will be 
notified. notify() method must be call from the synchronized context.
the thread calling the notify() method must hold lock on the object(Intance of class).

notifyAll() method call brings all the waiting thread for the particular object's lock into the runnable state and JVM will bring
any thread from the runnable to running state. notifyAll() method must be call from the synchronized context.
the thread calling the notifyAll() method must hold lock on the object.

sleep() method call puts the current running thread into sleeping state for the specified number of milli seconds
but the sleeping thread doesnt release the lock on the object it is working.

yield() method call puts the current running thread in the runnable state and give chance to another thread to run.
but it is not gauranteed that the same thread will not come back in running state.
The thread calls the yield() method will go into runnable state but it is possible that the same will be bring back
into running state by the JVM.

join() 
when one(FLOWER) thread calls a join method on another(FRUIT) thread then the current running(FLOWER) thread will wait for the
(FRUIT)thread it has joined finish its execution.

setPriority() method call set the priority for the current method calling thread. and the thread priority range is 1(min) to 10(max). (5 is norm).
if any thread has a parent thread then it will get the priority same as its parent.
the the priority range 1 to 10 can vary from JVM to JVM. but never higher or lower than 1 to 10.



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...