
This video will give you an overview about the course.
Running tasks in parallel can make a program run much faster. We will implement and execute our first parallel task, where the parallelism is achieved with just a few lines of code.
• Create your first parallel task - implement the Callable interface for now
• Get an instance of a ForkJoinPool, where the tasks will be executed
• Start the tasks in parallel with invokeAll()
Take our first parallel task one step further and combine results of individual subtasks into a single final result.
• Create a RecursiveTask
• Spin off subtasks with a fork() method
• Collect results with the join() method
RecursiveAction is powerful, but the ForkJoin framework has a lot more to offer. Here, we shall implement a new kind of a task and experiment with other methods provided by the framework.
• Go beyond RecursiveTask and explore ForkJoinTask
• Create a RecursiveAction
• Play with different methods on the ForkJoinPool
Life happens and sometimes exceptions are thrown. We will experiment with different exceptions in this video.
• Create a task throwing an unchecked exception, see how it affects other subtasks
• Handle a checked exception inside the ForkJoinTask.compute() method
• Cancel a task and see what happens with the execution
Callable and Future are the core abstractions for asynchronous computations. They represent a task and its future result, and create a foundation for almost all other multithreading abstractions.
• Execute a task in parallel, by creating a callable object and submitting it to an object pool
• Work with Future and learn about its lifecycle
• Create a Runnable action, and compare it to its callable counterpart
Thread is in the root of multithreading. This will be our first encounter with the concept of a thread.
• Create a thread object and learn about its basic properties
• Use a better way to create and manage threads – by utilizing thread pools
• Experiment with different kinds of Thread Pools
CompletableFuture takes Future to the next level, and allows us to create dependencies between different tasks. For example, to trigger execution of one task/stage on other task/stage’s successful completion.
• Create a CompletableFuture
• Chain several CompletableFuture objects with thenAccept(), thenCombine(), and thenApply()
• Handle exceptions with #handle()
Many manipulations with collections communicate better when written in functional-style. Java Streams allow us to use functional-style on collections making code more readable, and thus, more maintainable.
• Create streams from different data sources, such as arrays or collections
• Run different transformation and reduction operations
• See how intermediate operations are lazy, and how important it is to use a terminal operation
Parallelizing streams with big number of elements, or with long-running transformations may improve performance.
• Create parallel streams from an existing stream, or directly from a collection
• Learn when to choose parallel streams over sequential streams
• Recap section 2 and compare Thread Pool to ForkJoinPool
Flow API and Reactive Streams handle streams of data asynchronously.
• See how a Publisher sends data into a Subscriber, in an animation
• Learn about the role of the subscription object
• Get familiar with the supporting classes
Subscriber is one of the major components of the Flow API. It handles the data sent by the Publisher.
• Implement a Subscriber and subscribe it to the Publisher
• Dynamically request more items for the Subscriber
• Create a second Subscriber, to learn how several Subscribers receive data from the same Publisher
Publisher is another main component of the Flow API. We will learn several ways of implementing it.
• Create a new Publisher by implementing the Publisher interface
• Instantiate a SubmissionPublisher and issue new items with the submit() method
• Create a Publisher by extending SubmissionPublisher
Processor is an optional element which transforms the data items, and acts and an intermediary between the Publisher and other Subscribers.
• See what a processor does in an animation
• Implement a processor and add it to the code created in previous videos
• Recap the section by reviewing the major interfaces and classes of the Flow API
Thread is a fundamental abstraction in concurrency. Knowing how it works, is essential for writing concurrent programs.
• Create a thread and see how its state changes during its lifecycle
• Learn the difference between a Thread and a Process, which is a popular job interview question
• Learn about the challenges in multi-threaded programs
Threads share data which provide a lot of opportunities, but also create challenges. The synchronized statements and methods address it by ensuring mutually exclusive access.
• Fix an existing multi-threaded program by using a synchronized statement
• Create a synchronized instance method
• Create a synchronized static method
Synchronized blocks are useful, but they lack flexibility. For example, they require lock acquisition and release to occur in a block-structured way. More recent Lock interface provides more flexible and sophisticated thread synchronization mechanism, than simple “synchronized” statements.
• Rewrite the existing program with lock/ReentrantLock
• Use #tryLock() to acquire a lock, if it is available
• Optimize read operations with ReadWriteLock
Sometimes, a thread should be activated on a certain event. In that case, wait() and notify() come handy to coordinate threads.
• Optimize a busy-waiting thread by using Thread#onSpinWait()
• Coordinate threads with a combination of synchronized blocks and wait()/notify()
• Re-write the same code with Locks and Conditions
Working with concurrent threads can be challenging and can create a new class of errors. Knowing them is the first step for preventing them.
• See what a data race looks like
• Learn about race conditions
• Explore the family of liveness problems, such as deadlocks, Livelocks, and starvation
Providing thread-safety for a variable from a scratch is tedious, boring, and error-prone. Delegating thread-safety to already existing and well-tested data structures, allows us to save a lot of time during development and testing.
• Reimplement the counter with AtomicInteger. The new code has fewer lines of code and non-blocking
• See how compare-and-set operation enables optimistic concurrency pattern
• Learn about other atomic data structures
HashMap and LinkedList are probably the most widely used collections in Java, which is why, it is important to use efficient thread-safe implementation of these collections.
• Write thread-safe code with ConcurrentHashMap and ConcurrentLinkedQueue
• See how compare-and-set operation is used to ensure thread-safety in ConcurrentLinkedQueue
• Learn about other concurrent collections
Consumer-producer problem is common in concurrent programming. Fortunately, Java provides a family of efficient data structures for this widespread problem.
• Implement code using a queue, that waits until an element becomes available
• Re-write the message queue from the previous section with LinkedBlockingQueue. This code is more concise
• Learn about other blocking queue
Thread-safe collections used in read-heavy scenarios should be optimized for read operations, and it can tolerate less-efficient writes. Copy-on-write collections address this issue.
• Write code with CopyOnWriteArrayList
• Learn about approach used in copy-on-write collections
• Recap the entire section
This video will give a short summary of the entire course.
• Summarize what we have learned until now
Multi-core processors are everywhere—from super-computers to mobile devices right in your pocket. That's why a modern developer must know how to leverage the power of multithreading.
This course will teach you how to use parallelism and concurrency in Java. You will learn how to parallelize tasks and functions with the ForkJoin framework and Streams. You will also learn how to work with the very popular Reactive Streams recently introduced to Java. Furthermore, you will master concurrent collections and lower-level synchronization techniques with locks.
This course conveniently provides quizzes to evaluate your knowledge and help you retain the new concepts.
By the end of this practical training course, you will have the knowledge to write elegant programs for multicore computers with enhanced performance and improved responsiveness.
This tutorial covers the vital concepts of multithreading and concurrency in Java with useful examples. You'll work with multithreaded programs to learn core concurrency concepts and how to apply them to your own projects.
About the Author
Tatiana Fesenko is a senior Java developer with a proven track record of bringing products from concept to implementation and a background that includes working in four countries with collaborators from almost all continents in corporate, semi-public, and start-up environments. She is highly driven, thorough, and passionate about technology, and has been professionally using Java for more than 15 years.