Wednesday, May 4, 2011

wait() and notify()

If you are creating any object in java then jvm will provide lock for every object. By default the lock will be disabled. When the object is used to call any synchronized method then the lock will be enabled and will be handed over to the thread that is using the object.
If you are calling non-static synchronized method then the object that will be used to call the method will be locked. If you are calling a static synchronized method then the default object for the class will be locked.
If you are using synchronized block then the object that will be passed as parameter for the block will be blocked. If the object is blocked by one thread then it will not be used to access any synchronized method concurrently by other thread.
If you are calling a sleep() with the thread class then the thread will be moved to sleep state. If the thread is moved to sleep state and if the thread is moved to sleep state and if the thread is locking some object then the lock will not be released.

wait() & notify():-

wait and notify methods are available in the object class. If you are calling the wait() then it must be called from the synchronized context. When you are using the object to call the wait() then the thread that will be used to invoke the statement will be moved to wait state. If another thread is using same object to call the notify() then the thread from wait state will be moved to runnable state.
If you have multiple threads in the wait state that was using the corresponding object and if you want to move all the threads from wait to runnable state then you need to call notifyAll().

What is the difference between sleep() and wait()?

sleep() :- Sleep is defined in the thread class. It is a static method in the thread class. So, it can be used with the class name. If you are calling the sleep() and if the lock is enabled for any object it will not be released. Sleep() may be called from the synchronized context or from non-synchronized context. If you are calling the sleep() then you need to specify some time. After completing the specified time the thread will be moved to runnable state.

wait():- wait() is defined in object class. It is the instance method in the object class. So, you need to use some object to call the wait(). If you are calling the wait() then the lock will be released on the corresponding object. wait() must be called from the synchronized context. If you are calling the wait() then if you want you can specify the time or you can ignore. If you are specifying the time then after completing the specified time the thread will be moved to runnable state. If you want you can call notify() to move the state to runnable before completing the time. If you are calling the wait() without specifying time then explicitly you need to call the notify() to move the thread state to runnable state.

Thread Priority():-

Whenever the thread object will be created then for every thread object some integer value will be allocated. This value is known as priority for the thread. If you are not providing priority explicitly then it will get some default priority. The priority value will be between 1 to 10.

The priority some constant is defined in the thread class. One constant is

public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;

The thread with highest priority will be executed by the CPU first than the other threads. If you want to provide the priority for the thread explicitly then you can call the following methods wit the thread object.

public final void setPriority(int);

If you want to access the priority of the thread you can call this method.

public final int getPriority();

If you are providing the priority to the thread less than 1 or greater than 10, then your jvm will throw one runtime exception called java.lang.IllegalArgumentException.

The priority of the thread is used if the operating system is using the priority based scheduling algorithm. Otherwise the priority of the thread will not be used. If you are defining any thread then some name will be provided for the corresponding thread. If you are not providing the name explicitly then jvm will provide some value like
Thread -
will start from 0.

If you want to provide the name of the thread, then you can use the constructor from the thread class that has the parameter of string type or you can call the following method of the thread class to provide name explicitly.

public final void setName(String);

If you want to access the name of the thread then you can call the following method with thread object
public final String getName();

If you are creating any thread object then it will be added into some ThreadGroup. In java ThreadGroup is a class available in java.lang package. It indicates the collection of the thread. One thread group will be created by default when the jvm starts with the name "main". If you are creating thread without providing thread group information the thread will be added in the main thread group. If you want you can create your own thread group and you can add threads into the current thread group.

If you want to add the thread into custom thread group then you need to use the thread class constructor that have the parameter has thread group.
You can access the thread group information of the thread by calling the following method.
public final ThreadGroup getThreadGroup();


No comments:

Post a Comment