TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java Tutorials

Java Runnable Interface

Overview

In this tutorial, we will learn about Java Runnable Interface. JVM allows a java application to run multiple threads. There are two ways to create a new thread of execution.

  • Subclass Thread Class
  • Implement Runnable interface

One is to declare a class to be a subclass of Thread. This
subclass should override the run() method of class Thread.
An instance of the subclass can then be allocated and started.

Runnable Interface

The other way to create a thread is to declare a class that
implements the Runnable interface. That class then
implements the run() method. An instance of the class can
then be allocated, passed as an argument when creating
Thread, and started.

The Thread class itself implements the Runnable interface.

public class Thread implements Runnable {

…

}

Example

A class that implements the Runnable interface and overrides the run() method.

package com.testingdocs.java.tutorials;

public class ParallelTask implements Runnable {

 @Override
 public void run() {
 System.out.println("In ParallelTask : Thread Name::" + Thread.currentThread().getName());
 }
}

Runnable Interface

Demo Program

package com.testingdocs.java.tutorials;

/**
 *
 * Java program to demonstrate Runnable interface
 * Java Tutorials - www.TestingDocs.com
 */

public class RunnableDemo {

 public static void main(String args[]) {
 System.out.println("Demo ");
 ParallelTask task1 = new ParallelTask();
 new Thread(task1).start(); //Start Thread
 ParallelTask task2 = new ParallelTask();
 new Thread(task2).start(); //Start Thread
 }
}

 

The demo application created two threads and started them apart from the main thread.

Sample Output

Demo
In ParallelTask : Thread Name::Thread-0
In ParallelTask : Thread Name::Thread-1

—

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Related Posts

Download Greenfoot Windows

Java Tutorials /

Download & Install Greenfoot on Windows

Java Tutorials /

Java Static Code Analysis

Java Tutorials /

Java Testing Tools

Java Tutorials /

Handle Multiple Exceptions in Java

Exceptions_In_Java

Java Tutorials /

Exceptions in Java Programs

‹ Java Polymorphism› Java Stack Class

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version