ADVERTISEMENT
ADVERTISEMENT

Java program to create a thread by implementing a Runnable interface

Java example to create a thread by implementing a Runnable interface.
Submitted by Nidhi, on April 06, 2022

Problem Solution:

In this program, we will create a class and implement the Runnable interface and override the run() method.

Program/Source Code:

The source code to create a thread by implementing the Runnable interface is given below. The given program is compiled and executed successfully.

// Java program to create a thread by implementing
// Runnable interface

public class Main implements Runnable {
  public static void main(String[] args) {
    Main m = new Main();

    Thread thrd = new Thread(m);

    thrd.start();

    System.out.println("Outside the thread");
  }

  public void run() {
    System.out.println("Thread Executed");
  }
}

Output:

Outside the thread
Thread Executed

Explanation:

In the above program, we created a class Main by implementing the Runnable interface and overriding the run() method. The Main class also contains a method main(). The main() method is an entry point for the program. Here, we created objects of the Main class and bind with the Thread class. After that, we started the thread by calling the start() method of the Thread class.

Java Threading Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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.