Thread is a light weight process multiple process start in thread Thread first start main thread and end last main thread. Thread package define in java.lang package. Threads allows a program to operate more efficiently by doing multiple things at the same time. Threads can be used to perform complicated tasks in the background without interrupting the main program.

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 (Dead) − A runnable thread enters the terminated state when it completes its task or otherwise terminates.

    Thread [main , 5 , main]

     Main - thread name

    5 - priority (thread by default priority is 5)

    Main - thread group

    

Set Thread Name 

Example -

class Threads{

public static void main(String []arr){

Thread t=Thread.currentThread();

System.out.print(t);

t.setName("parent");

System.out.print(t);

}

}

Output -

    Thread[main,5,main]Thread[parent,5,main]


Pattern of Create a Thread

    1.  By Implementing Runnable Interface

    2.  By Extending Thread Class


Differences between "extending" and "implementing" Threads

The major difference is that when a class extends the Thread class, you cannot extend any other class, but by implementing the Runnable interface, it is possible to extend from another class as well, like: class MyClass extends OtherClass implements Runnable.


By Implementing Runnable Interface

If your class is intended to be executed as a thread then you can achieve this by implementing a Runnable interface.  If the class implements the Runnable interface, the thread can be run by passing an instance of the class to a Thread object's constructor and then calling the thread's start method.

Example -

class TDemo implements Runnable{

Thread t=null;

TDemo (String s){

t=new Thread(this,s);

t.start();

}

public void run(){

for(int i=11; i<=15; i++){

try{

System.out.print(i);

Thread.sleep(1000);

}

catch(InterruptedException e){

}

}

}

}

class Demo{

public static void main(String []arr){

TDemo t1=new TDemo("First");

for(int i=1; i<=5; i++){

try{

System.out.print(i);

Thread.sleep(1000);

}

catch(InterruptedException e){

}

}

}

}


By Extending Thread Class

If the class extends the Thread class, the thread can be run by creating an instance of the class and call its start() method.

Example -

class TDemo extends Thread{

TDemo (String s){

super(s);

start();

}

public void run(){

for(int i=11; i<=15; i++){

try{

System.out.print(i);

Thread.sleep(1000);

}

catch(InterruptedException e){

}

}

}

}

class Demo{

public static void main(String []arr){

TDemo t1=new TDemo("First");

for(int i=1; i<=5; i++){

try{

System.out.print(i);

Thread.sleep(1000);

}

catch(InterruptedException e){

}

}

}

}


Multithreading in Java

Multithreading in java is a process of executing multiple threads simultaneously.

A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking.

However, we use multithreading than multiprocessing because threads use a shared memory area. They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process.

Java Multithreading is mostly used in games, animation, etc.

Example -

class TDemo extends Thread{

TDemo (String s){

super(s);

start();

}

public void run(){

for(int i=11; i<=15; i++){

try{

System.out.print(Thread.currentThread().getName()+" "+i);

Thread.sleep(1000);

}

catch(InterruptedException e){

}

}

}

}

class Demo{

public static void main(String []arr){

TDemo t1=new TDemo("First");

TDemo t2=new TDemo("Second");

TDemo t3=new TDemo("Thired");

}

}

Set priority in Thread

Each thread have a priority. Priorities are represented by a number between 1 and 10. In most cases, thread schedular schedules the threads according to their priority (known as preemptive scheduling). But it is not guaranteed because it depends on JVM specification that which scheduling it chooses.

  1. public static int MIN_PRIORITY   -    1
  2. public static int NORM_PRIORITY    -    5
  3. public static int MAX_PRIORITY     -   10
Example -
class TDemo extends Thread{
TDemo (String s, int p){
super(s);
setPriority(p);
start();
}
public void run(){
for(int i=11; i<=15; i++){
try{
System.out.print(Thread.currentThread().getName()+" "+i);
Thread.sleep(1000);
}
catch(InterruptedException e){
}
}
}
}
class Demo{
public static void main(String []arr){
TDemo t1=new TDemo("First",3);
TDemo t2=new TDemo("Second",Thread.MAX_PRIORITY);
TDemo t3=new TDemo("Thired",Thread.NORM_PRIORITY+2);
}
}