What is thread?
Thread is a sequence of code executed independently with other threads of control with in a single executed program .
Java is a multithreaded programming language which means we can develop multithreaded program using Java.
A multithreaded program contains two or more parts that can run concurrently and each part can handle different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs.
Life Cycle of a Thread :
New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.
Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.
Waiting: Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task.A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.
Timed waiting: A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.
Terminated: A runnable thread enters the terminated state when it completes its task or otherwise terminates.
Threads can be implemented in two ways:
1.By implementing runnable interface.
2. By extending thread class.
If we extend thread class then we need to override "run" method of thread class as it contains the logic to execute the thread.
After that we need to execute start() method.
Other methods supported by Threads are given below.
JAVA THREAD PROGRAMS :
1. BY IMPLEMENTING RUNNABLE INTERFACE.
2. BY EXTENDING THREAD CLASS.
Thread is a sequence of code executed independently with other threads of control with in a single executed program .
Java is a multithreaded programming language which means we can develop multithreaded program using Java.
A multithreaded program contains two or more parts that can run concurrently and each part can handle different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs.
Life Cycle of a Thread :
New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.
Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.
Waiting: Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task.A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.
Timed waiting: A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.
Terminated: A runnable thread enters the terminated state when it completes its task or otherwise terminates.
Threads can be implemented in two ways:
1.By implementing runnable interface.
2. By extending thread class.
If we extend thread class then we need to override "run" method of thread class as it contains the logic to execute the thread.
After that we need to execute start() method.
Other methods supported by Threads are given below.
- join(): It makes to wait for this thread to die. You can wait for a thread to finish by calling its join() method.
- sleep(): It makes current executing thread to sleep for a specified interval of time. Time is in milli seconds.
- yield(): It makes current executing thread object to pause temporarily and gives control to other thread to execute.
- notify(): This method is inherited from Object class. This method wakes up a single thread that is waiting on this object's monitor to acquire lock.
- notifyAll(): This method is inherited from Object class. This method wakes up all threads that are waiting on this object's monitor to acquire lock.
- wait(): This method is inherited from Object class. This method makes current thread to wait until another thread invokes the notify() or the notifyAll() for this object.
JAVA THREAD PROGRAMS :
1. BY IMPLEMENTING RUNNABLE INTERFACE.
2. BY EXTENDING THREAD CLASS.
No comments:
Post a Comment