The document discusses threads, which are the smallest unit of execution in a process. It defines threads, their life cycle stages (new, runnable, running, wait/block, dead/terminated), and properties. There are two ways to create threads in Java - by inheriting the Thread class or implementing the Runnable interface. The Thread class extends Object and implements Runnable. When inheriting the Thread class, a class extends Thread and can override the run() method to provide the thread's implementation.
The document discusses threads, which are the smallest unit of execution in a process. It defines threads, their life cycle stages (new, runnable, running, wait/block, dead/terminated), and properties. There are two ways to create threads in Java - by inheriting the Thread class or implementing the Runnable interface. The Thread class extends Object and implements Runnable. When inheriting the Thread class, a class extends Thread and can override the run() method to provide the thread's implementation.
The smallest unit or sequence of execution is known as a
Thread.
Note :
1. Every thread has its own stack and PC ( Program Counter )
2. One Process can have multiple threads, it is known as multi- threading. 3. Multi-thread is required to achieve parallel execution. ( that is to execute more than one instruction at a same time ) 4. All the threads of the process share the resources of the process, That is all the threads share the same code segment, same data segment of a process. But will have a separate stack segment for execution
Thread Life Cycle :
new stage : When a Thread is just created it is in new State,
it remains to stay in the same stage until it is started. Runnable Stage: When a new thread is started, the thread goes to runnable stage, in this stage the thread is considered as ready for execution. Running Stage: The thread which is under execution is in the running stage. Wait stage / When the thread in the running is stopped, then Block stage the thread goes to the wait stage. The thread remains to be in the same stage until 1. another thread signals to the waiting thread. Intro To Threads Page 1 1. another thread signals to the waiting thread. 2. Timed Wait : it stays in this stage for a given specific time, after which the thread goes back to runnable stage. Dead stage / A thread goes to dead stage once the given task is Terminated Stage completed. ( if thread reaches this stage the stack of the thread is removed )
Properties of Thread :
1. Thread ID 2. Thread Name 3. Thread Priority
In Java, we can create threads in two ways :
Intro To Threads Page 2
1. by inheriting Thread class. 2. by implementing Runnable interface.
In Java we have a class java.lang.Thread which helps us to create
threads.
Note :
1. java.lang.Thread extends java.lang.Object class
2. java.lang.Thread implements Runnable interface
public class Thread extends Object implements Runnable
1st Method: By inheriting Thread Class
Step1 : class must extends Thread class.
Step2 : We can override run() method of thread class, to provide