Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Become a member Sign in Get started

Threads in Programming
Parathan Thiyagalingam Follow
Jun 14 · 3 min read

Have you ever wondered how big applications or software are working?
Imagine while you are playing video games you may have seen some
occurrences where changes occur concurrently like meter
measurements or the moving objects in the display. These won’t happen
at the same time. Each action is executed under a Thread. But those
thread execution changes within milliseconds, to our naked eye we
can’t observer what’s happening

Photo by Olliss on Unsplash

An application can have multiple processes, the process can have multiple
threads.

To know about Threads we need to know about Process.

Refer the following Wikipedia Definition for Thread and Process

In computer science, a thread of execution is the smallest sequence of


programmed instructions that can be managed independently by a
scheduler, which is typically a part of the operating system.

The implementation of threads and processes differs between operating


systems, but in most cases a thread is a component of a process.

Multiple threads can exist within one process, executing concurrently and
sharing resources such as memory, while different processes do not share
these resources.

In particular, the threads of a process share its executable code and the
values of its dynamically allocated variables and non-thread-local global
variables at any given time.

Process
The process starts with a single thread [Primary Thread] and later
can create more Thread from any of its Threads.

A process has at least one thread

All Threads of a Process have to access its memory and System


resources

Threads
The smallest unit of execution to which processor allocates time

Threads consist of the following

Program Counter: (contains the address of the next instruction to be


executed)

a Stack

Set of registers

A unique id

However, a thread itself is not a program. It cannot run on its own but
runs within a program

Let’s have a look at a simple explanation of the following picture to


understand Threads.

A web server is running on the internet. While running it will have a


primary thread inside a process. The process’s main intention is to
handle HTTP requests to the server.

When a new request comes it will create a new thread inside that process
and send a response to the request.

Threads VS Process
Threads are easier to create than processes since they don’t require a
separate address space.

Threads are considered lightweight because they use far fewer


resources than processes.

Processes are typically independent, while threads exist as subsets of


a process

Processes have separate address spaces, whereas threads share their


address space

Context switching between threads in the same process is typically


faster than context switching between processes.

If you want to know about context-switching refer this link to get to know
about it

Thread Life-cycle diagram

Thread has the following state while its execute

Ready

Running

Sleeping

Waiting

Dead

Blocked

When I/O request interrupted the execution it will move to Blocked state
and when the request is done will move to Ready state.

Methods in the arrow indication are used in Java to change the state of a
thread.

In the next article, we will see the coding implementation of


Thread in Java
Follow me on Twitter

Software Engineering Software Development T hreads Coding Software

WRIT T EN BY

Parathan Thiyagalingam Follow

A passionate in Tech https://www.linkedin.com/in/parathantl

Write the rst response

More From Medium

Securely Transfer Files My Favorite Free gRPC Client Context Switching in


From or to a Remote Courses to Learn Python Authentication Depth
Server Using SCP In-Depth Johan Brandhorst in Utility Buddhika Chathuranga in
Prashant Sharma in Better javinpaul in Better Warehouse RuntimeError
Programming Programming

Exploring Cost Programming Using Vapor and Fluent ASP.NET Core Custom
Optimisation on AWS FUNdamentals —Lists, to create a REST API Drop Down List
Aayush Kumar in FinOps Talks Loops, and User Input Radu Dan Dmitry Sikorsky
T heCyberBasics in Python In
Plain English

Discover Medium Make Medium yours Become a member


Welcome to a place where words matter. On Medium, smart Follow all the topics you care about, and we’ll deliver the Get unlimited access to the best stories on Medium — and
voices and original ideas take center stage - with no ads in best stories for you to your homepage and inbox. Explore support writers while you’re at it. Just $5/month. Upgrade
sight. Watch

About Help Legal

You might also like