Last Updated:

Threads in Java. Inherit from Thread, implement the Runnable interface

Process in Java

A process is a stand-alone runtime environment and can be thought of as a program or application. However, the program itself contains several processes within itself. For example, the Java runtime environment works as a single process that contains various classes and programs.

Thread in Java

Thread can be called a lightweight process. It requires fewer resources to create and exists in the process, sharing resources with it.

Multithreading in Java

Every Java program works with at least one thread, the main thread. Despite the fact that there are a lot of other threads running in the background: memory management, system management, signal processing, etc. But from the point of view of our program, the first thread will be the main one.

Multithreading is two or more threads running simultaneously in the same program. A computer with a single-core processor can execute only one thread, dividing the processor time between different processes and threads.

Benefits of Threads

  1. Threads are much lighter than processes, they require less time and resources.
  2. Context switching between threads is much faster than between processes.
  3. It is much easier to achieve interaction between threads than between processes.

Java provides two ways to programmatically create a stream.

  1. Implement the .java.lang.Runnable
  2. Class extension .java.lang.Thread

An example of creating a Thread. Implement the Runnable interface

In order for a class to be runnable, we must implement the interface and provide an implementation of the . To use this class as a thread, we have to create a Thread object by passing a runnable object to the class, and then call the method to execute on a separate thread.java.lang.Runnablepublic void run()start()run()

Here is an example of a Java class that implements the Runnable interface.

An example of creating a stream. Inheriting the Thread Class

We can inherit the class to create our own Thread class and override. Then we can create an instance of this class and call the method in order to execute.

java.lang.Threadrun()start()run()

Here's a simple example of how to inherit from the Thread class:

Here is a test program showing our flow in operation:

package com.p-qc;
public class ThreadRunExample {
public static void main(String[] args){
Thread t1 = new Thread(new HeavyWorkRunnable(), "t1");
Thread t2 = new Thread(new HeavyWorkRunnable(), "t2");
System.out.println("Starting runnable threads");
t1.start();
t2.start();
System.out.println("Runnable threads in progress");
Thread t3 = new MyThread("t3");
Thread t4 = new MyThread("t4");
System.out.println("Starting our custom streams");
t3.start();
t4.start();
System.out.println("Custom threads in progress");

}
}

Runnable vs Thread

If your class provides more than just running as a Thread, then you're better off implementing the Runnable interface. If you just need to run on a separate thread, you can inherit from the Thread class.

The Runnable implementation is preferable because Java supports the implementation of multiple interfaces. If you inherit from the Thread class, you can no longer inherit from other classes.

Interesting to know

Starting with Java 8, Runnable is a functional interface and we can use lambda expressions to implement it, instead of an anonymous class. Follow the updates of the site and you will see a complete guide to lambda expressions in Java!