×

Html & Html5

The World's best Software Development Study Portal

Multithread

Thread :-

It is smallest unit of process. A thread is a lightweight sub process.

Multithreading :-

The execution of multiple threads at the same time is called as multithreading.

Creating Thread :-

Thread can be created by 2 different ways , they are:-
  1. By extending Thread class
  2. By implementing Runnable interface

Few Methods Used In Thread class :-

  1. public void run() :- After the Thread is created , the code inside of the run() method is execute in the Thread.
  2. public void start() :- It starts the execution of the Thread.
  3. public void sleep(milliseconds) :- It stops the execution of Thread for the specified number of milliseconds.
  4. public void join() :-It puts the Thread on wait and waits for it to die.
  5. public void join(milliseconds) :- It waits for thread to die for specified milliseconds.
  6. public int getPriority() :- It returns the priority if the given Thread.
  7. public int setPriority(int Priority) :- It set the priority of the given Thread.
  8. public String getName() :- It returns the name of the Thread.
  9. public void setName(String name) :- It changes the name of the Thread.
  10. public void stop() :- It is used to stop the Thread.


Java Thread Example by extending Thread class

class Multithreadingexample extends Thread

{

public void run(){

System.out.println("Thread is created successfully");}

public static void main(String args[]) {

Multithreadingexample t1=new Multithreadingexample();

t1.start();

}

}

OUTPUT :-



Java Thread Example by implementing Runnable interface

class Multithreadingexample implements Runnable

{

public void run() {

System.out.println("Thread is created successfully"); }

public static void main(String args[]) {

Multithreadingexample m1=new Multithreadingexample();

Thread t1=new Thread(m1);

t1.start();

}

}

OUTPUT :-



Example of Sleep() Method In Java

class Sleepexample extends Thread{

public void run(){

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

i=i+1;

try{Thread.sleep(500);}

catch(InterruptedException e)

{System.out.println(e);}

System.out.println(i);

}

}

public static void main(String args[]){

Sleepexample t1=new Sleepexample ();

Sleepexample t2=new Sleepexample ();

t1.start();

t2.start();

}

}

OUTPUT :-



Example of Join() Method In Java :-

class Joinexample extends Thread{

public void run(){

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

i=i+1;

try{Thread.sleep(500);}

catch(InterruptedException e)

{System.out.println(e);}

System.out.println(i);

}

}

public static void main(String args[]){

Joinexample t1=new Joinexample();

Joinexample t2=new Joinexample();

Joinexample t3=new Joinexample();

t1.start();

try{

t1.join();}

catch(Exception e){

System.out.println(e);

}

t2.start();

t3.start();

}

}

OUTPUT



Example of Naming a Thread In Java :-

class Nameexample extends Thread{

public void run(){

System.out.println("Thread is created successfully");

}

public static void main(String args[]){

Nameexample t1=new Nameexample();

Nameexample t2=new Nameexample();

System.out.println("Name of Thread 1 is:- "+t1.getName());

System.out.println("Name of Thread 2 is:- "+t2.getName());

t1.start();

t2.start();

t1.setName("qwe");

t2.setName("asd");

System.out.println("After changing the name of thread 1:- "+t1.getName());

System.out.println("After changing the name of thread 2:- "+t2.getName());

}

}

OUTPUT



Thread Priority In Java :-

3 Constants Defined in Thread Class:-
  1. public static int MIN_PRIORITY
  2. public static int NORM_PRIORITY
  3. public static int Max_PRIORITY

Note:

Default priority of a thread is 5 (NORM_PRIORITY).

The value of MIN_PRIORITY is 1.

The value of MAX_PRIORITY is 10.

Example of Priority of a Thread In Java :-

class Prioritythread extends Thread{

public void run(){

System.out.println("running thread name is:"+Thread.currentThread().getName());

System.out.println("running thread priority is:"+Thread.currentThread().getPriority());

}

public static void main(String args[]){

Prioritythread m1=new Prioritythread();

Prioritythread m2=new Prioritythread();

m1.setPriority(Thread.MIN_PRIORITY);

m2.setPriority(Thread.MAX_PRIORITY);

m1.start();

m2.start();

}

}

OUTPUT



Daemon Thread in Java:-

It is a low priority thread which runs in the background. It does not prevents JVM from exiting when the program is finished but Thread is still running.

Example of Daemon Thread in Java:-

public class DaemonThread extends Thread{

public void run(){

if(Thread.currentThread().isDaemon()){

System.out.println("daemon thread work");

}

else{

System.out.println("user thread work");

}

}

public static void main(String[] args){

DaemonThread t1=new DaemonThread();

DaemonThread t2=new DaemonThread();

DaemonThread t3=new DaemonThread();

t1.setDaemon(true);

t1.start();

t2.start();

t3.start();

}

}

OUTPUT:-



Java Thread Pool:-

It is the collection of Threads which are waiting for a job and can be reuse many times.

Synchronization in Java:-

It is a method by which only one Thread can access the resource at a given time.

Example of Synchronization in Java:-

class Table{

synchronized void printTable(int n){

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

System.out.println(n*i);

try{

Thread.sleep(400);

}catch(Exception e){System.out.println(e);}

}

}

}

public class SynchronizationThread{

public static void main(String args[]){

final Table obj = new Table();

Thread t1=new Thread(){

public void run(){

obj.printTable(5);

}

};

Thread t2=new Thread(){

public void run(){

obj.printTable(100);

}

};

t1.start();

t2.start();

}

}

OUTPUT:-