2. All content
1. Basic thread management
2. Thread synchronization mechanisms
3. Thread creation and management
delegation with executors
4. Fork/Join farmework to enhance the
performance of your application
5. Data structures for concurrent programs
6. Adapting the default behavior of some
concurrency classes to your needs
7. Testing Java concurrency applications
3. In this chapter, we will cover:
● Creating and running a thread
● Getting and setting thread information
● Interrupting a thread
● Controlling the interruption of a thread
● Sleeping and resuming a thread
● Waiting for the finalization of a thread
● Creating and running a daemon thread
● Processing uncontrolled exceptions in a
● Using local thread variables
4. ● Grouping threads into a group
● Processing uncontrolled exceptions in a
group of threads
● Creating threads through a factory
5. Creating and running a thread
Threads are Objects. We have two ways of
creating a thread in Java:
● Extending the Thread class and overriding
the run() method
● Building a class that implements the
Runnable interface and then creating an
object of the Thread class passing the
Runnable object as a parameter
6. Creates and runs 10 threads.
Each thread calculates and prints the
mutiplication table of a number between 1 and
10. There’s more...
You can implement a class that extends
Thread class and overrides the run()
method of this class.
Call the start() method to have a new
11. Getting and setting thread
The Thread class saves some infomation
attributes that can help us to identify a thread:
● ID: A unique identifier for each Thread.
● Proiority: The priority of the Thread
value ∈ [1, 10] (low-->high)
Not recommended to change, but you can use
if you want.
12. ● Status: Thread can be in one of these six
states (new, runnable, blocked,
waiting, time watiing,
You can’t modify the ID and stauts of a thread.
13. Develop a program that establishes the name
and priority for 10 threads, and then show
information about their status until they finish.
14. How to do it...
15. New 10 threads and set the priority
16. Create log.txt file record the status
of 10 threads, and start them.
17. If we detect a change in the status of
a thread, we write them on the file
18. The writeThreadInfo method
19. How it works...
20. Interrupting a thread
Finish a program:
● All its non-daemon threads end its
● One of the threads use the System.exit()
21. Interruption mechanism
Thread has to:
● Check if it has been interrupted or not.
● Decide if it responds to the finalization
request or not.
Thread can ignore it and continue with its
22. Develop a program that creates Thread and,
after 5 seconds, will force its finalization using
the interruption mechanism.
23. How to do it...
24. How it works...
25. There’s more...
The Thread class has another method to
check whether Thread has been interrupted or
26. Difference: isInterrupted() and
● interrupted() is static and checks the
● isInterrupted() is an instance method
which checks the Thread object that it is
The second one doesn’t change the
interrupted attribute value, but the first one
set it to false.
27. Difference: isInterrupted() and
29. think about these...
● (1) and (3)
● (1) and (4)
● (2) and (3)
● (2) and (4)
30. Controlling the interruption of a
InterruptedException: A better
mechanism to control the interruption of the
Detect and throw this exception and catch in
the run() method.
31. We will implement Thread that looks for files
with a determined name in a
folder and in all its subfolders to show how to
use the InterruptedException exception
to control the interruption of a thread.
32. How to do it...
33. How it works...
34. There’s more...
The InterruptedException exception is
thrown by some Java methods related with the
concurrency API such as sleep()
35. Sleeping and resuming a thread
A thread in a program checks a sensor state
once per minute. The rest of the time, the
thread does nothing.
36. We will develop a program that uses the
sleep() method to write the actual
date every second.
37. How to do it...
38. How it works...
39. When Thread is sleeping and is interrupted,
the method throws an
exception immediately and doesn't wait until the
sleeping time finishes.
40. Waiting for the finalization of a
We can use the join() method of the Thread
When we call this method using a thread
object, it suspends the execution of the calling
thread until the object called finishes its
public final void join()
Waits for this thread to die.
join() ==> join(0)
InterruptedException - if any thread has
interrupted the current thread. The interrupted
status of the current thread is cleared when this
exception is thrown.
42. How to do it...
43. How it works...
44. There’s more...
Waits at most millis milliseconds for this thread
to die. A timeout of 0 means to wait forever.
join(long milliseconds, long nanos)
45. If the object thread1 has the code, thread1.
join(1000), the thread2 suspends its
execution until one of these two conditions is
● thread1 finishes its execution
● 1000 milliseconds have been passed
When one of these two conditions is true, the
join() method returns.
46. What’s the result?
47. How it works...
48. Creating and running a daemon
Java has a special kind of thread called
What is daemon thread?
49. Daemon thread
● Very low priority.
● Only executes when no other thread of the
same program is running.
● JVM ends the program finishing these
threads, when daemon threads are the only
threads running in a program.
50. What does daemon thread used for...
Normally used as service providers for normal
Usually have an infinite loop that waits for the
service request or performs the tasks of the
They can’t do important jobs.
Example: The java garbage collector.
51. Developing an example with two threads:
● One user thread that writes events on a
● One daemon thread that cleans the queue,
removing the events which were generated
more than 10 seconds age.
52. How to do it...
53. How it works...
If you analyze the output of one execution of
the program, you can see how the queue
to grow until it has 30 events and then, its size
will vary between 27 and 30 events until the
end of the execution.
54. Something wrong?!
55. Modify the run() method of
56. How it works...Think of the reason...
57. How to find out the reason...
● Modify the run() method of WriteTask,
make it easy to distinguish each element.
● Add a scanner, list all element of this deque.
58. Modify the run() method of
59. Add a scanner...
60. Modify main() method...
61. How it works...
62. Find out the reason...
● ArrayDeque: Not thread-safe
● We can use LinkedBlockingDeque…
Deque<Event> deque = new
63. There’s more...
● You only call the setDaemon() method
before you call the start() method. Once
the thread is running, you can’t modify its
● Use isDaemon() method to check if a
thread is a daemon thread or a user thread.
64. Difference between Daemon and
Non Daemon thread in Java :
● JVM doesn't wait for any daemon thread to
finish before existing.
● Daemon Thread are treated differently than
User Thread when JVM terminates, finally
blocks are not called, Stacks are not
unwounded and JVM just exits.
65. Processing uncontrolled exceptions
in a thread
There are two kinds of exceptions in Java:
● Checked exceptions
● Unchecked exceptions
66. Exceptions in run() method:
● Checked exception:
We have to catch and treat them, because
the run() method doesn't accept a throws
● Unchecked exception:
A mechanism to catch and treat the
The default behaviour is to write the stack
trace in the console and exit the program.
67. We will learn this mechanism using an
68. How to do it...
69. How it works...
If the thread has not got an uncaught exception
handler, the JVM prints the stack trace in the
console and exits the program.
70. There’s more...
Establishes an exception handler for all the
Thread objects in the application.
71. When an uncaught exception is
thrown in Thread...
JVM looks for…
1. The uncaught exception handler of the
2. The uncaught exception handler for
ThreadGroup of the Thread objects.
3. The default uncaught exception handler.
The JVM writes the stack trace of the exception
72. Using local thread variables
One of the most critical aspects of a concurrent
application is shared data.
If you change an attribute in a thread, all the
threads will be affected by this change.
The Java Concurrency API provides a clean
mechanism called thread-local variables with a
very good performance.
73. We will develop a program that has the
problem and another program using the thread-
local variables mechanism.
74. How to do it...
75. How it works...
Each Thread has a different start time but,
when they finish, all have the same value in its
76. How to do it...
77. How it works...
78. There’s more...
● protected T initialValue()
● public T get()
● public void set(T value)
● public void remove()
It provides inheritance of values for threads
created from a thread.
You can override the childValue() method
that is called to initialize the value of the child
thread in the thread-local variable.
80. How to do it...
81. How it works...
82. Grouping threads into a group
ThreadGroup: The threads of a group as a
A threadGroup object can be formed by
Thread objects and by another ThreadGroup
A tree structure of threads.
83. Simulating a search...
We will have 5 threads sleeping during a
random period of time, when one of them
finishes, we are going to interrupt the rest.
84. How to do it...
86. How it works...
87. Normal condition
88. Abnormal condition
The name attribute
changed three times.
Only list active thread.
89. Processing uncontrolled exceptions
in a group of threads
Establish a method that captures all the
uncaught exceptions thrown by any Thread of
the ThreadGroup class.
90. We will learn to set a handler using an
91. How to do it...
Declare a constructor and override the
92. How it works...
93. Creating threads through a factory
The factory pattern in OO is a creational
Develop an object whose creating other objects
of one or servel classes.
Instead of using the new operator.
Centralize the creation of objects.
94. Some advantages:
● It's easy to change the class of the objects
created or the way we create these objects.
● It's easy to limit the creation of objects for
limited resources. For example, we can only
have n objects of a type.
● It's easy to generate statistical data about
the creation of the objects.
95. Java provides the ThreadFactory interface to
implement a Thread Object factory.
Well will implement a ThreadFactory
interface to create Thread objects with a
personalized name while we save statistics of
the Thread objects created.
96. How to do it...
97. How it works...
98. There’s more...
If you implement a ThreadFactory interface
to centralize the creation of threads, you have
to review the code to guarantee that all threads
are created using that factory.