Threads
- Threads allows a program to operate more efficiently by doing multiple things at the same time.
Thread Life Cycle
- Every thread has a life cycle.
- During the life time of thread , there are many states it can enter.
• Newborn state
• Runnable state
• Running state
• Blocked state
• Dead state
1) Newborn state
- When we create a thread object, the thread is born and is said to be in newborn state.
- The thread is not yet scheduled for running.
- At this state, we can do only one of the following things with it:
• Schedule it for running using start( ) method.
• Kill it using stop() method.
- If scheduled , it moves to the runnable state.
- If we attempt to use any other method at this stage, an exception will be thrown.
2) Runnable state
- The runnable state means that the thread is ready for execution and is waiting for the availability of the processor.
- That is , the thread has joined the queue of threads that are waiting for execution.
- If all threads have equal priority, then they are given time slots for execution in round robin fashion , i.e. First-come , first-serve manner.
- The thread that relinquish control joins the queue at the end and again waits for its turn.
- This process of assigning time to threads is known as time-slicing.
- However , if we want a thread to relinquish control to another thread to equal priority before its turn comes , we can do so by using the yield() method as shown in
following figure.
3) Running state
- Running means that the processor has given its time to the thread for its execution.
- The thread runs until it relinquishes control on its own or it is preempted by a higher priority thread.
- A running thread may relinquish its control in one of the following situations.
- It has been suspended using suspend() method. A suspended thread can be revived by using the resume() method. This approach is useful when we want to suspend a thread for some time due to certain reason , but do not want to kill it.
- It has been made to sleep. We can put a thread to sleep for a specified time period using the method sleep(time) where time is in milliseconds. This means that the thread is out of the queue during this time period. The thread re-enters the runnable state as soon as this time period is elapsed.
- It has been told to wait until some event occurs. This is done using the wait() method. The thread can be scheduled to run again using the notify() method.
4) Blocked state
- A thread is said to be blocked when it is prevented from entering into the runnable state and subsequently the running state. This happens when the thread is suspended, sleeping or waiting in order to satisfy certain requirements.
- A blocked thread is considered “not runnable” but not dead and therefore fully qualified to run again.
- Every thread has a life cycle.
- A running thread ends its life when it has completed executing its run() method.
- It is a natural death
- However, we can kill it by sending the stop message to it at any state thus causing a premature death to it.
- A thread can be killed as soon it is born , or while it is running , or even when it is in “ not runnable ” (blocked) condition.
Types of threads:
1.Daemon Thread
- A daemon thread is a thread that does not prevent the JVM from exiting when the program finishes but the thread is still running. An example for a daemon thread is the garbage collection.
- You can use the setdaemon(boolean) method to change the Thread daemon properties before the thread starts.
- A Daemon thread is a background service thread which runs as a low priority thread and performs background operations like garbage collection. JVM exits if only daemon threads are remaining.
- The setdaemon() method of the Thread class is used to mark/set a particular thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads. This method must be called before the thread is started.
2.Non-deamon thread
- A Java program ends when all its threads finish (more specifically, when all its non-daemon threads finish).
- If the initial thread (the one that executes the main() method) ends, the rest of the threads will continue with their execution until they finish.
- If one of the threads use the System.exit() instruction to end the execution of the program, all the threads end their execution.
- Creating an object of the Thread class doesn't create a new execution thread.
- Also, calling the run() method of a class that implements the Runnable interface does not create a new execution thread. Only calling the start() method creates a new execution thread.





No comments:
Post a Comment