Загрузка...

#2 Print 1-N using M Threads | Trick to parameterise thread logic in 1 class |Multithreading in Java

𝐈𝐧 𝐭𝐡𝐢𝐬 𝐯𝐢𝐝𝐞𝐨, 𝐲𝐨𝐮'𝐥𝐥 𝐥𝐞𝐚𝐫𝐧 𝐡𝐨𝐰 𝐭𝐨 𝐩𝐫𝐢𝐧𝐭 𝐧𝐮𝐦𝐛𝐞𝐫𝐬 𝐟𝐫𝐨𝐦 𝟏 𝐭𝐨 𝐍 𝐮𝐬𝐢𝐧𝐠 𝐌 𝐭𝐡𝐫𝐞𝐚𝐝𝐬 𝐢𝐧 𝐉𝐚𝐯𝐚 — 𝐰𝐢𝐭𝐡 𝐟𝐮𝐥𝐥 𝐜𝐨𝐧𝐭𝐫𝐨𝐥 𝐨𝐯𝐞𝐫 𝐭𝐡𝐞 𝐞𝐱𝐞𝐜𝐮𝐭𝐢𝐨𝐧 𝐨𝐫𝐝𝐞𝐫 𝐮𝐬𝐢𝐧𝐠 𝘸𝘢𝘪𝘵()
𝐚𝐧𝐝 𝘯𝘰𝘵𝘪𝘧𝘺𝘈𝘭𝘭().

𝐋𝐞𝐜𝐭𝐮𝐫𝐞 𝐍𝐨𝐭𝐞𝐬: https://drive.google.com/file/d/1VSz_0I99_OGKg6_Oh6BxeDCQj786uUNZ/view?usp=sharing

This builds upon our previous Odd-Even Printer problem, and introduces a powerful generalization: using modular arithmetic and thread IDs to coordinate thread turns.

🔍 𝐊𝐞𝐲 𝐂𝐨𝐧𝐜𝐞𝐩𝐭𝐬 𝐂𝐨𝐯𝐞𝐫𝐞𝐝:
- How to combine Odd and Even Printer logic into a single class
- When to use while instead of if with wait()
- Why notifyAll() is safer than notify() in real-world scenarios
- Generalizing (current - 1) % totalThreads != threadId to scale to M threads
- A clean and elegant solution using one class for multiple threads

🚀 This video is part of the “Multithreading in Java” series where we take real-world problems and teach interview-level concepts clearly and practically.

𝐓𝐢𝐦𝐞𝐥𝐢𝐧𝐞𝐬:
00:00 - Intro: What we will achieve by end of this video
01:14 - Recap: Odd-Even Printer and challenge from last video
01:54 - Thread ID based approach explained on whiteboard
02:55 - Generalizing condition: (current - 1) % M != threadId
05:00 - Modifying java code to combine Odd/Even Printer classes
06:13 - Problem Statement print 1-N numbers using M threads
06:45 - Deep dive into solution.
07:09 - Co-relating Odd/Even Printer to current problem and deriving the formula
15:45 - Why use wait() with while() and not if() ?
19:05 - notify() vs notifyAll()
21:35 - Setting up agenda for next lecture

Github: https://github.com/AndyS12/Multithreading-in-Java/tree/main/Problems/src/EasyLevel/PrintUsingNThreads
LinkedIn: https://www.linkedin.com/in/anand-singh-sn/

#JavaMultithreading #ConcurrencyInJava #JavaInterviewQuestions #ThreadsInJava #waitnotify #print1ToN #threadSynchronization #JavaCodingInterview #CleanCode

Видео #2 Print 1-N using M Threads | Trick to parameterise thread logic in 1 class |Multithreading in Java канала Bits of Code by Anand
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять