0% found this document useful (0 votes)
18 views4 pages

Java Concurency

The document explains multithreading in Java, highlighting its definition as multiple threads executing within the same application, akin to having multiple CPUs. It discusses the benefits of multithreading, including better CPU utilization, improved user experience, and fairness in resource sharing. Additionally, it addresses the complexities and challenges of multithreading, particularly concerning shared memory access and the need for proper precautions to avoid unpredictable outcomes.

Uploaded by

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

Java Concurency

The document explains multithreading in Java, highlighting its definition as multiple threads executing within the same application, akin to having multiple CPUs. It discusses the benefits of multithreading, including better CPU utilization, improved user experience, and fairness in resource sharing. Additionally, it addresses the complexities and challenges of multithreading, particularly concerning shared memory access and the need for proper precautions to avoid unpredictable outcomes.

Uploaded by

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

Java Concurrency

What is Multithreading?
Multithreading means that you have multiple threads of execution inside the same
application. A thread is like a separate CPU executing your application. Thus, a
multithreaded application is like an application that has multiple CPUs executing
different parts of the code at the same time.

A thread is not equal to a CPU though. Usually a single CPU will share its execution
time among multiple threads, switching between executing each of the threads for a
given amount of time. It is also possible to have the threads of an application be
executed by different CPUs.
Why Multithreading?
There are several reasons as to why one would use multithreading in an application.
Some of the most common reasons for multithreading are:

 Better utilization of a single CPU.


 Better utilization of multiple CPUs or CPU cores.
 Better user experience with regards to responsiveness.
 Better user experience with regards to fairness.

I will explain each of these reasons in more detail in the following sections.

Better Utilization of a Single CPU

One of the most common reasons is to be able to better utilize the resources in the
computer. For instance, if one thread is waiting for the response to a request sent over
the network, then another thread could use the CPU in the meantime to do something
else. Additionally, if the computer has multiple CPUs, or if the CPU has multiple
execution cores, then multithreading can also help your application utilize these extra
CPU cores.

Better Utilization of Multiple CPUs or CPU Cores

If a computer contains multiple CPUs or the CPU contains multiple execution cores,
then you need to use multiple threads for your application to be able to utilize all of the
CPUs or CPU cores. A single thread can at most utilize a single CPU, and as I
mentioned above, sometimes not even completely utilize a single CPU.

Better User Experience with Regards to Responsiveness

Another reason to use multithreading is to provide a better user experience. For


instance, if you click on a button in a GUI and this results in a request being sent over
the network, then it matters which thread performs this request. If you use the same
thread that is also updating the GUI, then the user might experience the GUI "hanging"
while the GUI thread is waiting for the response for the request. Instead, such a request
could be performed by a backgroun thread so the GUI thread is free to respond to other
user requests in the meantime.

Better User Experience with Regards to Fairness

A fourth reason is to share resources of a computer more fairly among users. As


example imagine a server that receives requests from clients, and only has one thread
to execute these requests. If a client sends a requests that takes a long time to process,
then all other client's requests would have to wait until that one request has finished. By
having each client's request executed by its own thread then no single task can
monopolize the CPU completely.
Multithreading vs. Multitasking
Back in the old days a computer had a single CPU, and was only capable of executing
a single program at a time. Most smaller computers were not really powerful enough to
execute multiple programs at the same time, so it was not attempted. To be fair, many
mainframe systems have been able to execute multiple programs at a time for many
more years than personal computers.

Multitasking

Later came multitasking which meant that computers could execute multiple programs
(AKA tasks or processes) at the same time. It wasn't really "at the same time" though.
The single CPU was shared between the programs. The operating system would switch
between the programs running, executing each of them for a little while before
switching.

Along with multitasking came new challenges for software developers. Programs can
no longer assume to have all the CPU time available, nor all memory or any other
computer resources. A "good citizen" program should release all resources it is no
longer using, so other programs can use them.

Multithreading

Later yet came multithreading which mean that you could have multiple threads of
execution inside the same program. A thread of execution can be thought of as a CPU
executing the program. When you have multiple threads executing the same program,
it is like having multiple CPUs execute within the same program.
Multithreading is Hard
Multithreading can be a great way to increase the performance of some types of
programs. However, mulithreading is even more challenging than multitasking. The
threads are executing within the same program and are hence reading and writing the
same memory simultaneously. This can result in errors not seen in a singlethreaded
program. Some of these errors may not be seen on single CPU machines, because two
threads never really execute "simultaneously". Modern computers, though, come with
multi core CPUs, and even with multiple CPUs too. This means that separate threads
can be executed by separate cores or CPUs simultaneously.

If a thread reads a memory location while another thread writes to it, what value will the
first thread end up reading? The old value? The value written by the second thread? Or
a value that is a mix between the two? Or, if two threads are writing to the same
memory location simultaneously, what value will be left when they are done? The value
written by the first thread? The value written by the second thread? Or a mix of the two
values written?

Without proper precautions any of these outcomes are possible. The behaviour would
not even be predictable. The outcome could change from time to time. Therefore it is
important as a developer to know how to take the right precautions - meaning learning
to control how threads access shared resources like memory, files, databases etc. That
is one of the topics this Java concurrency tutorial addresses.

Full Blog : https://jenkov.com/tutorials/java-concurrency/index.html

You might also like