DEV Community

gauharnawab
gauharnawab

Posted on

MULTITHREADING IN JAVA

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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay