What is Thread
- A thread is a single sequential flow of control within a program
- Facility to allow multiple activities within a single process
- Referred as lightweight process
- Each thread has its own program counter, stack and local variables
- Threads share memory, heap area, files
Why to use Thread
- To perform asynchronous or background processing
- Increases the responsiveness of GUI applications
- Better utilization of system resources
- Simplify program logic when there are multiple independent entities
- A very good example of a multi-threaded application is game software where the GUI display, updating of scores, sound effects and timer display are happening within the same application simultaneously.
- Other Thread Advantages
- Threads are memory efficient. Many threads can be efficiently contained within a single EXE, while each process will incur the overhead of an entire EXE.
- Threads share a common program space, which among other things, means that messages can be passed by queuing only a pointer to the message.
- Thread task switching time is faster, since a thread has less context to save than a process.
Threads can exist in different states which are below –
- When an object is created (using new), then that thread is said to be New born thread.
- Just because a thread’s start() has been called, it does not mean that the thread has access to the CPU and can start executing straightaway.
- Running state – This means that the CPU is currently executing the thread. The thread scheduler decides which thread is in the running state.
Non Runnable states : A thread can go from the Running state into one of the non-runnable states, depending on the transition.
- Waiting state – a thread can call the wait() defined in Object class to put itself into the waiting state until it gets some resource held by some other thread. It must be notified by another thread in order to move to Runnable state.
- Sleeping state – calls sleep() and frees CPU for a specific time. It wakes up after a specified amount of time has elapsed and transits to the Runnable state.
- Blocked state – A running thread on executing a blocking operation requiring a resource (like a call to an I/O method) will move the thread to the blocking state. The blocking operation must complete, before the thread can proceed to Runnable state. When a thread wants to read some data from a file stream or wants to write data into a stream where the stream is used by another thread, then this thread has to wait till the OS stores data in the buffer and reads it.
it can call suspend and resume for blocked state.
Yield() – a thread can go from running to runnable state by calling yield() so that it can behave like a polite thread.
When a thread is executing a long loop to ensure that it is not monopolizing (control) the system, it can call yield().
- Runnable state – Ready to run. A thread does not go directly to the Running state from one of the Non –Runnable state . It first goes to the Runnable state, meaning that it is now ready for execution.
- Yield() – A call to the static method yield() will cause the current running thread to move to the Runnable (Ready-to-run),
thus relinquishing (give up) the CPU. It can allow other threads to execute not being selfish.
Dead state – The thread can be dead b’cos it has completed or it has been terminated. Once in this state, the thread cannot be resurrected (make active).
A new thread of execution must be explicitly started by calling the start().