All Posts

Multi-threading in Java with Examples

Multi-threading in Java with Examples

Multi-threading is a process of executing multiple threads in parallel. In this tutorial, we will learn about Java Threads.

What is a Thread?

A thread (sometimes also called a lightweight process) is a basic processing unit to which an OS(Operating System) allocates the processor time. The JVM(Java Virtual Machine) allows the Java application to run multiple threads concurrently.

Each thread has a priority. Threads set with higher priority are executed before threads with lower priority.

Create a Thread

There are two ways to create a thread in Java.

  • Subclass Thread
  • Implement Runnable Interface

Thread class

One approach is to define a class to be a subclass of Thread. This subclass should override the run() method of the class Thread.

Runnable Interface

The second approach is to declare a class that implements the Runnable interface. The class can then implement the run() method. An instance of the class can then be allocated and passed as an argument when creating a Thread.

—

Java Tutorials

Java Tutorials on this website:

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