[java concurrency]01.thread management
DESCRIPTION
java concurrencyTRANSCRIPT
![Page 2: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/2.jpg)
All content
1. Basic thread management2. Thread synchronization mechanisms3. Thread creation and management
delegation with executors4. Fork/Join farmework to enhance the
performance of your application5. Data structures for concurrent programs6. Adapting the default behavior of some
concurrency classes to your needs 7. Testing Java concurrency applications
![Page 3: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/3.jpg)
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
thread● Using local thread variables
![Page 4: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/4.jpg)
● Grouping threads into a group● Processing uncontrolled exceptions in a
group of threads● Creating threads through a factory
![Page 5: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/5.jpg)
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
![Page 6: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/6.jpg)
Creates and runs 10 threads.
Each thread calculates and prints the mutiplication table of a number between 1 and 10.
![Page 7: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/7.jpg)
How to do it...
![Page 8: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/8.jpg)
![Page 9: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/9.jpg)
How it works...
![Page 10: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/10.jpg)
System.exit(n)
Runtime.getRuntime().exit(n)0/?(1~127, 128~255, <0)
![Page 11: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/11.jpg)
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 execution thread.
![Page 12: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/12.jpg)
Getting and setting thread information
The Thread class saves some infomation attributes that can help us to identify a thread:● ID: A unique identifier for each Thread.● Name● Proiority: The priority of the Thread
objects.value ∈ [1, 10] (low-->high)
IllegalArgumentExceptionNot recommended to change, but you can use if you want.
![Page 13: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/13.jpg)
● Status: Thread can be in one of these six states (new, runnable, blocked, waiting, time watiing, terminated)
You can’t modify the ID and stauts of a thread.
![Page 14: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/14.jpg)
Develop a program that establishes the name and priority for 10 threads, and then show information about their status until they finish.
![Page 15: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/15.jpg)
How to do it...
![Page 16: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/16.jpg)
New 10 threads and set the priority of them
![Page 17: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/17.jpg)
Create log.txt file record the status of 10 threads, and start them.
![Page 18: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/18.jpg)
If we detect a change in the status of a thread, we write them on the file
![Page 19: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/19.jpg)
The writeThreadInfo method
![Page 20: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/20.jpg)
How it works...
![Page 21: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/21.jpg)
![Page 22: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/22.jpg)
Interrupting a thread
Finish a program:● All its non-daemon threads end its
execution.● One of the threads use the System.exit()
method.
Interruption mechanism
![Page 23: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/23.jpg)
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 execution.
![Page 24: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/24.jpg)
Develop a program that creates Thread and, after 5 seconds, will force its finalization using the interruption mechanism.
![Page 25: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/25.jpg)
How to do it...
![Page 26: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/26.jpg)
![Page 27: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/27.jpg)
How it works...
![Page 28: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/28.jpg)
There’s more...
The Thread class has another method to check whether Thread has been interrupted or not.Thread.interrupted()
![Page 29: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/29.jpg)
Difference: isInterrupted() and interrupted()
● interrupted() is static and checks the current thread.
● isInterrupted() is an instance method which checks the Thread object that it is called on.
NOTE:The second one doesn’t change the interrupted attribute value, but the first one set it to false.
![Page 30: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/30.jpg)
Difference: isInterrupted() and interrupted()
![Page 31: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/31.jpg)
isInterrupted(boolean ClearInterrupted)
![Page 32: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/32.jpg)
think about these...
● (1) and (3)● (1) and (4)● (2) and (3)● (2) and (4)
![Page 33: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/33.jpg)
Controlling the interruption of a thread
InterruptedException: A better mechanism to control the interruption of the thread. Detect and throw this exception and catch in the run() method.
![Page 34: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/34.jpg)
We will implement Thread that looks for files with a determined name in afolder and in all its subfolders to show how to use the InterruptedException exceptionto control the interruption of a thread.
![Page 35: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/35.jpg)
How to do it...
![Page 36: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/36.jpg)
![Page 37: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/37.jpg)
![Page 38: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/38.jpg)
![Page 39: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/39.jpg)
![Page 40: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/40.jpg)
How it works...
![Page 41: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/41.jpg)
There’s more...
The InterruptedException exception is thrown by some Java methods related with the concurrency API such as sleep()
![Page 42: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/42.jpg)
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. ● Thread.sleep()● TimeUnit.SECONDS.sleep()
![Page 43: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/43.jpg)
We will develop a program that uses the sleep() method to write the actualdate every second.
![Page 44: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/44.jpg)
How to do it...
![Page 45: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/45.jpg)
![Page 46: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/46.jpg)
How it works...
![Page 47: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/47.jpg)
When Thread is sleeping and is interrupted, the method throws an InterruptedExceptionexception immediately and doesn't wait until the sleeping time finishes.
![Page 48: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/48.jpg)
Waiting for the finalization of a thread
We can use the join() method of the Thread class.When we call this method using a thread object, it suspends the execution of the calling thread until the object called finishes its execution.
![Page 49: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/49.jpg)
join()
public final void join() throws InterruptedExceptionWaits for this thread to die.join() ==> join(0)Throws:InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
![Page 50: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/50.jpg)
How to do it...
![Page 51: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/51.jpg)
![Page 52: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/52.jpg)
How it works...
![Page 53: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/53.jpg)
There’s more...
join(long milliseconds)Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
join(long milliseconds, long nanos)
![Page 54: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/54.jpg)
If the object thread1 has the code, thread1.join(1000), the thread2 suspends its execution until one of these two conditions is true:● thread1 finishes its execution● 1000 milliseconds have been passed
When one of these two conditions is true, the join() method returns.
![Page 55: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/55.jpg)
What’s the result?
![Page 56: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/56.jpg)
How it works...
![Page 57: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/57.jpg)
Creating and running a daemon thread
Java has a special kind of thread called daemon thread.
What is daemon thread?
![Page 58: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/58.jpg)
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.
![Page 59: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/59.jpg)
What does daemon thread used for...
Normally used as service providers for normal threads.Usually have an infinite loop that waits for the service request or performs the tasks of the thread.They can’t do important jobs.
Example: The java garbage collector.
![Page 60: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/60.jpg)
Developing an example with two threads:● One user thread that writes events on a
queue.● One daemon thread that cleans the queue,
removing the events which were generated more than 10 seconds age.
![Page 61: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/61.jpg)
How to do it...
![Page 62: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/62.jpg)
![Page 63: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/63.jpg)
![Page 64: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/64.jpg)
![Page 65: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/65.jpg)
![Page 66: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/66.jpg)
![Page 67: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/67.jpg)
How it works...
If you analyze the output of one execution of the program, you can see how the queue beginsto grow until it has 30 events and then, its size will vary between 27 and 30 events until theend of the execution.
![Page 68: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/68.jpg)
Something wrong?!
![Page 69: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/69.jpg)
Modify the run() method of WriteTask.java ...
![Page 70: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/70.jpg)
How it works...Think of the reason...
![Page 71: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/71.jpg)
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.
![Page 72: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/72.jpg)
Modify the run() method of WriteTask
![Page 73: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/73.jpg)
Add a scanner...
![Page 74: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/74.jpg)
Modify main() method...
![Page 75: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/75.jpg)
How it works...
![Page 76: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/76.jpg)
Find out the reason...
● ArrayDeque: Not thread-safe● We can use LinkedBlockingDeque…Deque<Event> deque = new LinkedBlockingDeque<Event>();
![Page 77: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/77.jpg)
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 daemon status.
● Use isDaemon() method to check if a thread is a daemon thread or a user thread.
![Page 78: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/78.jpg)
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.
![Page 79: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/79.jpg)
Processing uncontrolled exceptions in a thread
There are two kinds of exceptions in Java:● Checked exceptions
IOExceptionClassNotFoundExceptionURLReferenceException
● Unchecked exceptionsNumberFormatExceptionClassCastExceptionNullPointExceptionOutOfMemoryError
![Page 80: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/80.jpg)
Exceptions in run() method:
● Checked exception:We have to catch and treat them, because
the run() method doesn't accept a throws clause. ● Unchecked exception:
A mechanism to catch and treat the unchecked exceptions.
The default behaviour is to write the stack trace in the console and exit the program.
![Page 81: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/81.jpg)
We will learn this mechanism using an example.
![Page 82: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/82.jpg)
How to do it...
![Page 83: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/83.jpg)
![Page 84: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/84.jpg)
![Page 85: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/85.jpg)
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.
![Page 86: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/86.jpg)
There’s more...
● setDefaultUncaughtExceptionHndler()
Establishes an exception handler for all the Thread objects in the application.
![Page 87: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/87.jpg)
When an uncaught exception is thrown in Thread...
JVM looks for…1. The uncaught exception handler of the
Thread objects.2. The uncaught exception handler for
ThreadGroup of the Thread objects.3. The default uncaught exception handler.
None handlers…The JVM writes the stack trace of the exception and exits.
![Page 88: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/88.jpg)
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.
![Page 89: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/89.jpg)
We will develop a program that has the problem and another program using the thread-local variables mechanism.
![Page 90: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/90.jpg)
How to do it...
![Page 91: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/91.jpg)
![Page 92: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/92.jpg)
How it works...
Each Thread has a different start time but, when they finish, all have the same value in itsstartDate attribute.
![Page 93: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/93.jpg)
How to do it...
![Page 94: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/94.jpg)
![Page 95: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/95.jpg)
How it works...
![Page 96: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/96.jpg)
There’s more...
● protected T initialValue()● public T get()● public void set(T value)● public void remove()● InheritableThreadLocal● childValue()
![Page 97: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/97.jpg)
InheritableThreadLocal
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.
![Page 98: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/98.jpg)
How to do it...
![Page 99: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/99.jpg)
![Page 100: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/100.jpg)
How it works...
![Page 101: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/101.jpg)
Grouping threads into a group
ThreadGroup: The threads of a group as a single unit.A threadGroup object can be formed by Thread objects and by another ThreadGroup object.A tree structure of threads.
![Page 102: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/102.jpg)
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.
![Page 103: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/103.jpg)
How to do it...
![Page 104: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/104.jpg)
SearchTask.java
![Page 105: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/105.jpg)
![Page 106: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/106.jpg)
![Page 107: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/107.jpg)
How it works...
Normal condition...Abnormal condition...
![Page 108: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/108.jpg)
Normal condition
Note:enumerate method:“activeCount”
![Page 109: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/109.jpg)
Abnormal condition
The name attribute changed three times.
threadGroup.list():Only list active thread.
![Page 110: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/110.jpg)
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.
![Page 111: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/111.jpg)
We will learn to set a handler using an example.
![Page 112: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/112.jpg)
How to do it...
Declare a constructor and override the uncaughtException().
![Page 113: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/113.jpg)
![Page 114: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/114.jpg)
![Page 115: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/115.jpg)
How it works...
![Page 116: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/116.jpg)
Creating threads through a factory
The factory pattern in OO is a creational pattern.Develop an object whose creating other objects of one or servel classes.Instead of using the new operator.Centralize the creation of objects.
![Page 117: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/117.jpg)
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.
![Page 118: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/118.jpg)
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.
![Page 119: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/119.jpg)
How to do it...
![Page 120: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/120.jpg)
![Page 121: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/121.jpg)
![Page 122: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/122.jpg)
How it works...
![Page 123: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/123.jpg)
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.
![Page 124: [Java concurrency]01.thread management](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b71b1d4a795991018b45ab/html5/thumbnails/124.jpg)
End
Thank you!