Home » Java programming language

Java Thread Class public void run() method with Example

Java Thread Class public void run() method: Here, we are going to learn about the public void run() method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 29, 2019

Thread Class public void run()

  • This method is available in package java.lang.Thread.run().
  • run() method of the thread contains the executable code of the thread.
  • This method is not static so we cannot access this method with the class name too.
  • Thread class contains run() method with empty implementation.
  • We can overload run() method in our class but Thread class start() call only default run() method by default and if we want to call another run () method then we need to call explicitly like a normal method.
  • If we override the run() method in our class then it contains the task so our thread is responsible to execute this method.
  • If we don't override run() method in our class then run() method will be executed of Thread class and we will not get any output because Thread class define run() method with empty implementation.
  • The return type of this method is void so it does not return anything.

Syntax:

    public void run(){
    }

Parameter(s):

When we write t.start(), so this line means call start() method of Thread and Thread class start() will call run() method of our defined class.

Return value:

The return type of this method is void, it does not return anything.

Java program to demonstrate example of run() method

/*  We will use Thread class methods so we are 
    importing the package but it is not mandate 
    because it is imported by default
*/
import java.lang.Thread;

class MyThread extends Thread {
    // Override run() method of Thread class
    public void run() {
        System.out.println("We are in run() method of MyThread thread");
    }
}

class Main {
    public static void main(String[] args) {

        // Here we are calling run() method of MyThread 
        // class like a normal method
        MyThread mt = new MyThread();
        mt.run();

        // Here we are calling start() method of Thread class 
        // and it will call a run() method of MyThread
        mt.start();

        // Here we are calling run() method of Thread class
        Thread t = new Thread();
        t.run();
    }
}

Output

E:\Programs>javac Main.java

E:\Programs>java Main
We are in run() method of MyThread thread
We are in run() method of MyThread thread






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.