TSession 9: Multi-Threading (2.
5 hours)
In this Session, we will cover Multithreading and Concurrency.
● Introduction and Learning Objectives (2 min)
● Concept - Concurrency (10 min)
○ Need
○ Different ways to achieve Concurrency and differences between them
■ Multiprogramming
■ Multitasking
■ Multiprocessing
■ Multithreading
○ Thread Vs Process
○ Advantages and Disadvantages of Multithreaded Applications
○ Different ways of creating threads and when to use when
■ Extending Thread
■ Implementing Runnable
○ run() Vs start()
○ [Link]() method
○ Follow up Questions (Poll Questions)
● Coding Problems - Creating Threads and Running them Concurrently (10 min)
○ Create thread on both ways
○ Show benefits of each ways
● Hands-On Experience - Learners has to create threads in both ways (10 min)
● Concept - Synchronization (5 min)
○ Need (Race Condition)
○ Monitor concept
○ Ways to achieve synchronization
■ Synchronized Methods
■ Synchronized Blocks
○ Synchronized Methods Vs Synchronized Block
○ Follow up Questions (Poll Questions)
● Coding Questions - Build Java application using Synchronization (5 min)
● Hands-On Experience - Learners has to use Synchronized Methods and Synchronized Block (10 min)
● Concept - Inter-thread Communication using wait and notify/notifyAll (5 min)
○ Need
○ What each method does (Using monitor)
○ Explain why wait, notify and notifyAll methods are parts of Object class
○ Follow up Questions (Poll Questions)
● Coding Questions - Code some kind of producer consumer problem (10 min)
● Hands-On Experience - Learners has to code some kind of producer consumer problem (10 min)
● Concept - Volatile and ThreadLocal (5 min)
● Concept - Executor Framework and Callable (5 min)
○ Need
○ Executor Framework and ThreadPool
○ Callable tasks
○ execute() and submit() methods
○ Future and FutureTask
○ Follow up Questions (Poll Questions)
● Coding Questions - Java application using Callable and Future (5 min)
● Hands-On Experience - Learners has to solve some problem using Callable and Future (15 min)
● Concept - Thread Safe Collections (5 min)
○ Need
○ Different ways to get thread-safe collections
■ Synchronized Collections
■ Concurrent Collections
○ Follow up Questions (Poll Questions)
● Coding Questions - Use thread-safe collections (5 min)
● Hands-On Experience - Learners to use one of the thread-safe collections (10 min)
● Summarize (5 min)
● Doubt Resolution Window (10 min)
● Conceptual Questions - (10 min)
○ What do you understand by Multithreading and Multitasking?
○ What is the difference between Thread and Process in Java?
○ When to use Runnable vs Thread in Java?
○ What is the difference between start() and run() method of Thread class?
○ What happens when an Exception occurs in a thread?
○ What is FutureTask in Java?
○ What is the difference between synchronized and concurrent collection in Java?
○ What is the ThreadLocal class? How and why would you use it?