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.



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