DEV Community


Posted on


Multithreading in Java for Beginners

In Java multithreading is a process of executing multiple threads simultaneously.

Before we understand what is Thread in Java we must know about process, kernel, and context switching.

  • Process:

A process is a sequence of execution of a program or we can say that it is an abstraction of hardware and kernel.

  • Kernel:

A kernel is an interface or fundamental component of the operating system that acts between software applications and the computer hardware.

  • Context Switching: In an operating system, context switching is the process between user mode to kernel mode to execute the system call.

What is Thread in Java?

we know already that thread is a lightweight process, but it is a sub-task of a process that shares the same address space as that process.

In Java we can achieve Multithreading in 3 ways:

Thread Class:

Code using Thread Class

Runnable Interface:

-> In Java runnable interface is used to define the task that can
be run as a thread. It has only one method void run() that can
be called when the thread is started.
-> It is a functional interface.

-> Functional Interface:
A functional Interface in Java is an interface that contains
only one abstract method.

Using Runnable Interface

Lambda Expression:
A lambda expression is a short block of code that takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented
right in the body of a method

Using Lambda Expression

Top comments (0)