
The thread implements a complex algorithm divided into some methods, or it has methods with recursive calls. Java provides the InterruptedException exception.
The thread doesn't use any resources of the computer.Use the sleep method of the Thread class.
In some situations, we will have to wait for the end of the execution of a thread (the run() method ends its execution). We can use the join() method of the Thread class.
Java has a special kind of thread called daemon thread. Daemon threads are normally used as service providers for normal threads running in the same program.
The Java programming language, as almost all modern programming languages, implements an exception-based mechanism to manage error situations.
An interesting functionality offered by the concurrency API of Java is the ability to group threads.Java provides the ThreadGroup class to work with a group of threads.
The factory pattern is one of the most used design patterns, and its objective is to develop an object whose mission is to create other objects of one or several classes. Java provides the ThreadFactory interface to implement a thread object factory.
A classic problem in concurrent programming is the producer-consumer problem. We will implement the producer-consumer problem using thesynchronized keyword and the wait(), notify(), and notifyAll() methods.
One of the most significant improvements offered by locks is the ReadWriteLock interface and the ReentrantReadWriteLock class; the unique class that implements that interface.
A classic problem in concurrent programming is the producer-consumer problem. We will implement the producer-consumer problem using locks and conditions.
The StampedLock class provides a special kind of lock that is different from the ones provided by the Lock or ReadWriteLock interfaces.
The Java concurrency API provides a class that allows one or more threads to wait until aset of operations are done. It's called the CountDownLatch class.
The Java concurrency API provides a synchronizing utility that allows the synchronization of two or more threads at a determined point. It's the CyclicBarrier class.
One of the most complex and powerful functionalities offered by the Java concurrency API is the ability to execute concurrent-phased tasks using the Phaser class. This mechanism is useful when we have some concurrent tasks divided into steps.
The Java concurrency API provides a synchronization utility that allows the exchange of data between two concurrent tasks.The Exchanger class allows you to have adefinition of a synchronization point between two threads.
The Java 8 Concurrency API includes a new synchronization mechanism with the CompletableFuture class. This class implements the Future object and the CompletionStage interface.
This video gives overview of the entire course.
One of the advantages of the Executor framework is that it allows you to run concurrent tasks that return a result. The Java Concurrency API achieves this with Callable and Future interfaces.
A common problem in concurrent programming arises when you have various concurrent tasks available to solve a problem. You have various sort of algorithms. You can launch all of them and get the result of the first one that sorts the array, that is, the fastest sorting algorithm for a given array.
There are use cases when you are not interested in executing a task as soon as possible. You may want to execute a task after a period of time or do it periodically; the Executor framework provides the ScheduledExecutorService interface along with its implementation, namely, the ScheduledThreadPoolExecutor class.
The Executor framework provides the ThreadPoolExecutor class to execute concurrent tasks using a pool of threads that helps you avoid all thread creation operations.
When you want to cancel a task that you send to the executor, you can use the cancel() method of Future, which allows you to make the cancelation operation.
The Java API provides the FutureTask class as a cancelable asynchronous computation. It implements the Runnable and Future interfaces and provides the basic implementation of the Future interface.
When you execute concurrent tasks using an executor, you will send Runnable or Callable tasks to the executor and get Future objects to control the method. For such situations, the Java Concurrency API provides the CompletionService class.
The fork/join framework provides the ability to execute tasks that return a result.
In this video, you will learn how to use the asynchronous methods provided by the ForkJoinPool and CountedCompleter classes for the management of tasks.
The behavior of the ForkJoinTask and ForkJoinPool classes is different. The program doesn't finish execution, and you won't see any information about the exception in the console. In this video, you will learn the techniques to get that information.
When you execute the ForkJoinTask objects in a ForkJoinPool class, you can cancel them before they start their execution. The ForkJoinTask class provides the cancel() Method.
In this video, you will learn how to use the different versions of the reduce() method to generate a result from a stream of values.
One of the most commons actions you will apply to a stream will be the filtering operation that selects the elements that continue with the processing. In this recipe, you will learn the different methods provided by the Stream class to select the elements of a stream.
One interesting option provided by the Stream class is the possibility to check whether the elements of the stream verify a condition or not.
The main difference between blocking deques and non-blocking deques is that blocking deques have methods to insert and delete elements that, if not done immediately. Java includes the LinkedBlockingDeque class that implements a blocking deque.
Another important characteristic of PriorityBlockingQueue is that it's a blocking data structure. It has methods that, if unable to perform the operation immediately, will block the thread until they are able to do it.
An interesting data structure provided by the Java API, which you can use in concurrent applications, is implemented in the DelayQueue class. In this program, we will learn to use the DelaydQueue class by storing in it some events with different activation dates.
The Java API provides a class that implements ConcurrentSkipListMap, which is the interface that implements a non-blocking list with the behavior of the ConcurrentNavigableMap interface.
A hash table is a data structure that allows you to map a key to a value. The Java API provides different hash table implementations through the Map and ConcurrentMap interfaces.
Java introduced atomic variables. When a thread is performing an operation with an atomic variable and if other threads want to do an operation with the same variable, the implementation of the class includes a mechanism to check that the operation is done atomically.
Variable handles are a new feature of Java 9 that allow you to get a typed reference to a variable. You can use variable handles to obtain the same functionality without using any synchronization mechanism. A variable handle also allows you to get additional access modes to a variable.
The executor uses one of its pooled threads or creates a new one to execute the tasks. It also decides the moment in which the task is executed.
Java provides theThreadFactory interface to implement a Thread object factory. Some advanced utilities of the Java concurrency API, such as the Executor framework or the fork/join framework, use thread factories to create threads.
The Executor framework is a mechanism that allows you to separate thread creation and its execution. It's based on the Executor and ExecutorService interfaces and the ThreadPoolExecutor class that implements both these interfaces.
In the Java Concurrency API, locks are declared in the Lock interface and implemented in some classes, for example, the ReentrantLock class
In this video, we will implement a data structure which is to be used in the producer / consumer problem; whose elements will be ordered by priority.
In this video, we will extend an atomic object and implement two operations that follow the mechanisms of the atomic objects to guarantee that all the operations are done in one step
Streams are based on the Stream interface and some related classes and interfaces included in the java.util.stream package. In this video, we will implement our own Spliterator interface and create a Stream interface to process its data
One of the most complex and powerful functionalities offered by the Java Concurrency API is the ability to execute concurrent-phased tasks using the Phaser class. This mechanism is useful when we have some concurrent tasks divided into steps.
A stream in Java is a sequence of elements that could be processed either parallelly or sequentially in a pipeline of declarative operations using lambda expressions.
A log system is a mechanism that allows you to write information to one or more destinations. In this video, we will use the classes provided by the java.util.logging package to add a log system to your concurrent application.
Static code analysis tools are a set of tools that analyze the source code of an application while looking for potential errors. The objective is to find errors or places that cause poor performance at an early stage, before they are executed in production.
MultithreadedTC is a Java library for testing concurrent applications. it includes an internal metronome. These testing threads are implemented as methods of a class.
JConsole is a monitoring tool that follows the JMX specification that allows you to get information about the execution of an application as the number of threads, memory use, or class loading. In this video, we will use this tool to monitor a simple concurrent application
Locks allow the definition of a critical section that only one thread can execute at a time. In this video, we will implement an example to see the difference in the performance of a task with a long operation inside the critical section and a task with a long operation outside the critical section.
The use of executors has a lot of advantages over direct utilization of threads. In this video, we will implement an example that shows how you can obtain better performance using an executor than creating the threads yourself.
Java 7 provides a new kind of executor with the fork/join framework. This executor, implemented in the ForkJoinPool class, is designed for problems that can be split into smaller parts using the divide and conquer technique. For these kinds of problems, fork/join pools get better performance than classical executors.
A Stream interface is a sequence of elements that can be filtered and transformed to get a final result sequentially or in parallel. In this video, we will process big data sets using streams.
Writing concurrent and parallel programming applications is a must-have skill for any Java programmer. Java 9 comes with a host of fantastic features, including significant performance improvements and new APIs. So, if you're familiar with the basics of Java and want to understand concurrency and parallel programming techniques, then go for this Learning Path.
Packt’s Video Learning Paths are a series of individual video products put together in a logical and stepwise manner such that each video builds on the skills learned in the video before it.
The highlights of this Learning Path are:
Let's take a look at your learning journey. You will know the elements of the Java concurrency API that will help you take advantage of the exciting new capabilities. You will learn how to use parallel and reactive streams to process massive data sets. Next, you will move on to create streams and use all their intermediate and terminal operations to process big collections of data in a parallel and functional way. Moving ahead, you’ll discover a whole range of recipes for almost everything, such as thread management, synchronization, executors, parallel and reactive streams, and much more. By the end of this Learning Path, you will be able to deploy scalable and concurrent application
Meet Your Expert:
We have the best works of the following esteemed author to ensure that your learning journey is smooth:
Javier Fernández González is a software architect with almost 15 years experience in Java technologies. He has worked as a teacher, researcher, programmer, analyst, and writer, and he now works as an architect in all types of projects related to Java, especially J2EE. As a teacher has over 1,000 hours of training in basic Java, J2EE, and the Struts framework. As a researcher, he has worked in the field of information retrieval, developing applications for processing large amounts of data in Java, and has participated as a co-author in several journal articles and conference presentations. Recently, he worked on developing J2EE web applications for various clients from different sectors (public administration, insurance, healthcare, transportation, and so on). He has also worked as a software architect. He is the author of the books, Java 7 Concurrency Cookbook and Mastering Concurrency Programming with Java 8 by Packt Publishing.