Java Concurency
Java Concurency
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:
I will explain each of these reasons in more detail in the following sections.
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.
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.
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.