0% found this document useful (0 votes)
4 views10 pages

012ProcessandThreadinJava

The document explains the concepts of processes and threads in Java, highlighting that a process is a heavyweight program with its own memory space, while a thread is a lightweight process that shares memory within a single process. It discusses the advantages of multithreading over single-threading, such as improved responsiveness, resource sharing, and efficient utilization of multiprocessor architectures. Additionally, it outlines the differences between process-based and thread-based multitasking, emphasizing their respective context switching costs and address space management.

Uploaded by

madhus639627
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

012ProcessandThreadinJava

The document explains the concepts of processes and threads in Java, highlighting that a process is a heavyweight program with its own memory space, while a thread is a lightweight process that shares memory within a single process. It discusses the advantages of multithreading over single-threading, such as improved responsiveness, resource sharing, and efficient utilization of multiprocessor architectures. Additionally, it outlines the differences between process-based and thread-based multitasking, emphasizing their respective context switching costs and address space management.

Uploaded by

madhus639627
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Process and Thread in Java

Process
It is a program that is running on your computer. The process is a heavy-weight,
which takes memory separately to other processes. The process may be a small
background task like spell-checker; it may be internet explorer or Microsoft word.
All processes have one or more threads. The process performs a series of
operations on a set of data.
The processes are created, scheduled, and terminated by the operating system
for the use of the CPU. The operations of the process are controlled with the help
of PCB (Process Control Block). PCB can be considered as the brain of the
process having all the crucial information of a process like its process id, priority,
state, contents CPU register, and PWS.
PCB is considered as the kernel-based data structure, which provides the three
kinds of functions which are scheduling, dispatching, and context save.
Scheduling: It chooses(decides) which process is (run)executed first in the CPU.
Dispatching: An environment is set up for the execution of the process.
Context save: It saves the information regarding a process when it gets
resumed or blocked.
The process is a program in execution. The operating system has to allocate the
resources; every process uses these resources and releases the resources on
the process termination. These increase the responsibility of the operating
system to provide all these resources to a process. In turn, the operating system
has a burden to manage these processes and to schedule the process. If there
are many processes created in the system, a point will come where the operating
system will not be able to allocate these resources to any process, and in turn,
the system will get hung. To solve this concept of threads was implemented.
The process life cycle has certain states included, such as ready, running,
blocked, and terminated. For consisting of the track of the processing activity at
an instant, its states are used.
Processes provide the medium to achieve the concurrent execution of a
program. The child process is created by the chief process of a concurrent
program. The main process and child process need to interact with each other
for completing the common task.
Feature of Process

1. The process has process id, CPU registers, memory locations, stack.
2. The process has its own virtual address space.
3. It provides protected access to processors, other processes, files, and I/O.
4. The process is a heavyweight.
5. The creation of a process includes system calls for each process
separately.
6. A single process does not share data and information and takes its
isolated memory space.
7. The IPC (Inter-process communication) mechanism for communication is
used by the processes, which significantly increases the number of system
calls.
8. More system calls are consumed by Process management.
9. Every process contains the stack and heap memory, instruction, data, and
memory map.

Thread
A thread is a lightweight process that shares the memory of a single process. It
supports Multithreading. Each thread relates to exactly one process, and no
thread can exist outside a process. Threads represent a software approach to
improve the performance of the operating system. They also specify a suitable
mechanism for parallel execution of applications on shared-memory
multiprocessors. Threads are not independent of one another; that is why they
share their code section, data section, and OS resources (like open files and
signals) with each other. But a thread has its program counter (PC), register set,
and stack space just like a process. The kernel provides a stack and a thread
control block (TCB) to every thread. The Threads mainly have the states called
the running, ready, and blocked states; it only includes computational state, not
resource allocation, and communication state, which reduces the switching
overhead. It enhances the concurrency (parallelism) because the many threads
are the parts of the single process. So, each thread is assigned to do the task to
speed up to complete the task.
When multiple threads are active, JVM provides the default priorities to each
thread. The priority of the thread specifies the time available for execution
relative to other active threads in the same process.
Feature of Thread:

1. As a single process is executing for a single task, a thread may be part of


the process, and it may execute a part of the process; hence it becomes
the lightweight process. Thus, a single process may have many such
threads that will take care of one part of the execution of that whole
process. So, every thread that created or that is generated in a process
also has its characteristics.
2. Thread comprises a thread id, a program counter, a register set, and a
stack as processes have process id, CPU registers, memory locations.
The thread id is distinguished every thread.
3. Threads share the resources of the process and are executing some part
of the process
4. Processes with threads are categories as following: The process may have
the one or multithreaded.
5. More than one thread (Lightweight process) is created by only one system
call.
6. Thread management does account for no or fewer system calls because
communication between threads can be achieved using shared memory.

7. The process’s isolation property increases its overhead in terms of resource


consumption.
Single-threaded
Single-threaded refers to executing an entire process from beginning to end
without interruption by a single thread. Here, every process is associated with
registers, stack, code, data, files, which is used by a single thread. This process
contains a single thread that is executing only one work for a process at a time.

For Example: If a web server is a single-threaded process, so web server will


handle only a single request at a time, and all other users have to wait till this one
request completed.
Multithreaded
Multithreaded refers to allowing multiple threads within a process so that they are
executed independently while sharing their resources.
Here every thread will have its registers and stack that will store the information
or store the work done by these particular threads in these registers and the
stack.

As this is the multithreaded process, the threads execute in the same resources
that are being allotted to a process; hence multithreaded process or a thread is
said to be the lightweight process as it does not require in additional resources to
execute.
For example:

1. In the Multithreaded web server, every thread will be assigned to each


user, and every user will able to access the required information from the
webserver.
2. Similarly, the word processor will have a thread to access the data from
the user simultaneously. Another thread will execute to find out the error,
spelling mistakes that will be typed by the user, whereas other threads
engage in the loading of the files, which are required in the word
processor.

Benefits of Multithreaded over Single threaded:

Responsiveness
By using Multithreading, it allows a program to run even if part of it is blocked. A
program continuously runs also if the process is performing a lengthy operation.
As many threads execute simultaneously for any single process, it increases the
responsiveness of a process as many threads doing the same job will improve
the response of the process. In an interactive application, we allow the program
to continue running even if a part is blocked or is performing some lengthy
operation; still, the application will respond because of Multithreaded.
Resource sharing
A single application can have many threads within the same address space for
sharing the resources that are allocated to the process.
As threads are the parts of the same process, they share the memory and
resource of the process to which they belong. The thread belongs to a specific
process that is executing some instruction, so these allow the benefit of code
sharing for an application so different threads may execute various activities for
an application in the same address space.
Economy
To create processes relates to more expensive and time-consuming as they
require more memory and resources as compared to threads. Management and
creation are much higher than thread creation and management.
Every thread executes in the process to which it belongs. The threads do not
have to allocate the new memory space or the resources, so they reduce the
resource utilization of the operating system. There is not much more need for
context switching between the processes as every thread is the part of the same
process and executing a particular part of the process, so the context switching
of the process is reduced, and that in turn reduces the resource requirement of
any operating system. Thus, it removes the overhead for creating and
maintaining a process rather than a thread.
Utilization of multiprocessor architectures
In this architecture, each thread can run on a different processor in parallel using
multithreading. This increases the concurrency of the system. In contrast, a
single processor system, where only one process or thread can run on a
processor at a time.
We can utilize the full power of multiprocessor architectures where every thread
can be assigned to a processor for some job. The multiple processors in the
multiprocessor systems will execute one single job that may be part of a big
processor, so this increases the speed of execution or speed of executing the
processes highly by running parallel instructions on a different processor.
A Single-threaded process will run only on the one processor even there are
many processors available for execution. In contrast, the Multithreaded process
will utilize all the processors that are available in the system.
What is the difference between Process and the Thread?
Process Thread

The process is resource-intensive or Thread is taking lesser resources than


heavyweight. a process and lightweight.

The switching of the thread need not


Process switching requires interaction
require to interact with the operating
with the operating system.
system.

All process has the same code but has its All threads are sharing the same set of
own private memory and file resources in resources and open files of the
multiple processing environments. processes.

There is no other process can execute If one thread is blocked and waiting,
the other thread in the same task can
until the first process is unblocked.
run.

Without using threads, use more Multiple threaded processes use fewer
resources by the process. resources.

Threads are not independent among


Every process operates independently of
themselves as one thread can read,
the others.
write, or change another thread’s data.

How does the Thread differ from the Process?


Faster context switch
A context switch occurs when the computer’s CPU switches(move) from a
process or thread to a different process or thread. Context switching allows a
CPU to handle multiple processes or threads without the need for additional
processors. A (computer’s CPU) processor follows to change (switches) from
one task to another while ensuring that tasks do not conflict.
Context switching involves storing the state or context of a process or thread so
that it can be reloaded when the need arises, and execution can be resumed
from the same point. This is the multitasking feature of the operating system and
permits a single processor to be shared by multiple processes.
Context switching time between threads is lower compared to process context
switch because threads relate to the shared memory as compared to the
process. Process context switching consists of more overhead of the CPU as
compared to threads context switching.
Communication
To communicate between multiple threads is easy as they share common
address space (share memory). At the same time in the process, we have to
follow some specific communication techniques (i.e., more system calls, etc.) for
communication between processes because they do not share the memory
space.
Enhanced throughput of the system
In the Multithreading mechanism, each thread has a function that is considered
as one job, then the number of jobs completed per unit of time is increased, thus
increasing the throughput of the system.
Resource sharing
Among all threads within a process, share resources like code, data, and files.
But the process does not share anything.
Effective utilization of multiprocessor system
If a single process has multiple threads, then we can schedule these threads on
multiple processors. This mechanism will lead to process execution faster. But
the complete process that are assigns to a processor takes time to finish
according to its activity completion. The threads of-course increase the efficiency
inside the process, but different processes complete after depends upon its
activities to terminate.
Multitasking in Java
Multitasking is the mechanism in which a user performs more than one computer
task at a time. The operating system can keep track of where you are in these
tasks and go from one to the other without losing information. It is the way of
executing several multiple tasks simultaneously. The operating systems which
involve in the Multitasking are Microsoft Windows 2000, IBM ’s OS/390, and
Linux.
Real-Life Example - classroom student is the best way of executing Multitasking.

1. Students are listening to class.


2. They are taking running notes of essential points.
3. They are checking the mobile phone in the class by many students.
4. They are observing the teacher’s way of teaching.

Types of Multitasking
1. Process-based Multitasking
2. Thread-based Multitasking.
Process-based Multitasking: Executing several multiple tasks simultaneously,
where each task is a separate independent program(process), is the process-
based Multitasking.
Example –All these three activities will be performed simultaneously, and each
task is independent of each other.

1. Typing the java program in the editor


2. Listening to the songs from the same system.
3. Download a file from the internet.

While typing a java program in the editor, we can listen to audio songs from the
same system at the same time we can download your file from the net. All these
tasks will be executed simultaneously and independently of each other hence it is
process-based Multitasking. It is better for the operating system level.
Thread based Multitasking:
It is the process of executing several tasks simultaneously where each task is a
separate independent part of the same program. Such type of Multitasking is
called Thread based Multitasking, and each independent part is called Thread. It
is suitable for programming level
Example: In a program, there are 10,000 lines of words. The first 5000 lines of
code are independent to other 5000 lines of code that means first related to the
first customer and second relates to another customer, so no need to wait to
execute second customer lines of code after first lines of code. Both parts can be
run simultaneously of the same program, so hardly less time to take to complete
the execution. The first independent part and second independent part executing
separate tasks simultaneously where each part is a separate independent part of
the same program, and each independent task is Thread This type of
Multitasking is Multithreading.
What is the difference between the Process and Thread based
Multitasking?
Process-based Multitasking Thread-based Multitasking

Context switching is costing Context switching is not more costing

There is a separate address space


Every thread shares one address space.
for every process.

Each task is a separate independent Each task is the separate individual part of
process. the same program.

You might also like