Java Concurrency - The Basics PDF
Java Concurrency - The Basics PDF
Java Concurrency - The Basics PDF
T OP S T ORY S UBMIT
You have 2 free stories left this month. Sign up and get an extra one for free.
This article describes the basics of Java Concurrency API, and how to
assign work to be done asynchronously.
We call “system thread” the thread created by the JVM that runs in the
background of the application (e.g. garbage-collector), and a “user-
defined thread” those which are created by the developer.
First, we need to know that in a standard Java program, all the work we
implement is done in the main thread, so the work is done sequentially.
To change that behavior, we can create our own threads and program
them to do the work “at the same time”, creating a multi-threaded
environment.
. . .
Given the two classes “Ping” and “Pong” below, they simulates 3 time
processing job that takes from 0 to 4 seconds to accomplish each one:
If we instantiate them and call each method run(), the work will be done
sequentially in the main thread, which is not our intention here:
Output is always:
But if we start the threads calling its start method, the work will be done
asynchronously and there is no guarantee of the execution order.
. . .
. . .
To ensure that the main thread finish its execution after the worker
threads, we can join it, pausing its execution while the worker threads
are alive.
The output may be as follows, and its guaranteed that the last line will be
the “End of main thread” in the example above.
Threads has an attribute name, that we can use to identify them during
program execution. The default name is “Thread-n”, where ’n’ is the
incremental number of creation.
class Counter {
int count;
public void increment() {
count++; // count = count + 1
}
}
And the main class that consumes it using two async threads:
The output varies on a number equals or less than 1000. The issue is on
the ‘cont’ attribute, that can had been incremented by the other thread
while it's being read by the first one. To ensure that just one thread will
be executing the ‘increment()’ method, we can use the synchronized
keyword, so we always get the correct sum of values (‘1000’, in this case).
class Counter {
int count;
public synchronized void increment() {
count++; // count = count + 1
}
}
. . .
I am pool-1-thread-2
I am pool-1-thread-4
I am pool-1-thread-1
I am pool-1-thread-3
Example of output:
I am pool-1-thread-1
I am pool-1-thread-2
I am pool-1-thread-3
There is a lot more, like thread safe data structures and scheduled tasks,
but I guess this content covers the very basics of Java concurrent API.
88 claps
WRIT T EN BY
Software Craftsman
FizzBuzz in MySQL Tests Are for the Future Tutorial: A Simple Simple CSS
Using Stored Tyler Hawkins in Better Framework For methodology
Procedures Programming Optimization Andy Dłubak
Jude Dutton Programming In Python
Using PuLP, Gurobi, and
CPLEX
Ehsan Khodabandeh
Decode JSON in Swift Concrete Example of [Tensor ow 101] What Using Bash for
with intermediate types Web Scraping with does it mean to reduce templating
Jeroen de Vrind in Short Swift Financial Data axis? con guration les in
Stories Alex-Adrien Auger in Sipios Aerin Kim Docker containers
Ivan Tuzhilkin in T he Startup