Methods of Multithreading | Join | Sleep | Notify | Notify All | Suspend | Resume - Java2021 : Online Tutorials Hub

Java2021 : Online Tutorials Hub

JAVA | Advance Java | MySQL | Data Structure | Git | HTML | CSS | JS

Latest

Post Top Ad

Monday, July 6, 2020

Methods of Multithreading | Join | Sleep | Notify | Notify All | Suspend | Resume

Here we will learn about methods in Multithreading 
  • Join Methods
  • Sleep Method
  • Notify Method
  • Notify All
  • Resume Method
  • Suspend Method
Learn about Sleep method : 
  • Sleep Method will print a thread value with sleeping time(like 600)
  • Thread Sleep specified amount of time
  • we will give a time millisecond (given in example or like 600ms)
  • exception occur so we will add Exception Object
Example : 

class ExampleOfSleep extends Thread{
    public void run(){
        for (int i=0;i<6;i++){
            try {
                Thread.sleep(600);
            }
            catch (InterruptedException e){
                System.out.println(e);
            }
            System.out.print(i+" ");
        }
    }
    public static void main(String[] args) {
        ExampleOfSleep e1=new ExampleOfSleep();
        e1.run();
    }
}
Output : 




Learn about Join Method : Join method will waiting thread to die.when thread is die scheduler pick another thread available in queue.
Example : 

public class ExampleOfJoin extends Thread{

    public void run() {
        for (int i = 0; i < 6; i++) {
            try {
                Thread.sleep(400);
            } catch (InterruptedException e) {
                System.out.println(e);
            }
            System.out.print(i+" ");
        }
    }
    public static void main(String[] args) {
        ExampleOfJoin e1=new ExampleOfJoin();
        ExampleOfJoin e2=new ExampleOfJoin();
        ExampleOfJoin e3=new ExampleOfJoin();
        e1.start();
        try {
            e1.join();
        }
        catch (Exception e){
            System.out.println(e);
        }
        e2.start();
        e3.start();
    }
}
Output : 



More About Important Method is used in Multithreadings : 
  • start() : start method is used to execution of the thread
  • run() : run method is used to do task for thread
  • getPriority : getPriority method is used to return priority of thread
  • setPriority : setPriority method is used to set the priority of thread
  • setName() : setName method is used to set the name of thread
  • getName() : getName method is used to get the name of thread
  • getId() : getId method is used to get id of thread
  • isAlive : isAlive method is used to test the if the thread is alive
  • suspend() : suspend method is used to suspend the thread
  • resume() : resume method is used to resume the suspended thread
  • stop() : stop method is used to stop the thread
  • notify() : notify method is used to notify one thread which is waiting for a particular object
  • notifyAll() : notifyAll method is used All  thread which is waiting for a particular object.

Sorting and Searching Programs

Start with your Choice

-Important Programs for Freshers

Learn Pattern Programs 

Click to learn Array Programs

Click to Learn MySQL 

for any complaint regarding my Blog please visit contact us page and write what problem you have!


No comments:

Pages