29. Multithreading and Concurrency in Java: Part1 | Threads, Process and their Memory Model in depth
ฝัง
- เผยแพร่เมื่อ 30 ก.ย. 2024
- ➡️ Notes: Shared in the Member Community Post (If you are Member of this channel, then pls check the Member community post, i have shared the Notes link there)
➡️ Join this channel to get access to member only perks:
/ @conceptandcoding
Overall Important Subtopics of Multithreading and Concurrency:
----------------------------------------------------------------------------------------------------------
- Introduction of Multithreading: (Covered in this Video)
* Definition of Multithreading
* Benefits and Challenges of Multithreading
* Processes v/s Threads
* Multithreading in Java
- Java Memory Model of Process and thread (Covered in this Video)
- Basics of Threads - Part1:
* Creating Threads
* Extending the Thread Class
* Implementing the Runnable Interface
* Thread Lifecycle
* New
* Runnable
* Blocked
* Waiting
* Timed Waiting
* Terminated
- Basics of Thread - Part2 : Inter Thread Communication and Synchronization
* Synchronization and Thread Safety
* Synchronized Methods
* Synchronized Blocks
* Inter-Thread Communication
* wait(), notify(), and notifyAll() methods
* Producer-Consumer Problem - Assingment
- Basics of Threads - Part3
* Producer-Consumer Problem - Solution discuss
* Stop, Resume, Suspended method is deprecated, understand why and its solution
* Thread Joining
* Volatile Keyword
* Thread Priority and Daemon Threads
- Some Advanced Topics
* Thread Pools
* Executor Framework
* ThreadPoolExecutor
* Callable and Future
* Fork/Join Framework
* ThreadLocal in Multithreading
- Concurrency Utilities
* java.util.concurrent Package
* Executors and ExecutorService
* Callable and Future
* CompletableFuture
* ScheduledExecutorService
* CountDownLatch, CyclicBarrier, Phaser, and Exchanger
- Concurrent Collections (already discussed during Collections topic, will provide working example for this)
* ConcurrentHashMap
* ConcurrentLinkedQueue and ConcurrentLinkedDeque
* CopyOnWriteArrayList
* BlockingQueue Interface
* ArrayBlockingQueue
* LinkedBlockingQueue
* PriorityBlockingQueue
- Atomic Variables
* AtomicInteger, AtomicLong, and AtomicBoolean
* AtomicReference and AtomicReferenceArray
* Compare-and-Swap Operations
- Locks and Semaphores
* ReentrantLock
* ReadWriteLock
* StampedLock
* Semaphores
* Lock and Condition Interface
- Parallel Streams (already discussed during Stream topic, will provide working example for this)
- Best Practices and Patterns
* Thread Safety Best Practices
* Immutable Objects
* ThreadLocal Usage
* Double-Checked Locking and its Issues
* Concurrency Design Patterns
- Common Concurrency Issues and Solutions
* Deadlocks
* Starvation
* Livelocks
* Race Conditions
* Strategies for Avoiding Concurrency Issues
- Java 9+ Features
* Reactive Programming with Flow API
* CompletableFuture Enhancements
* Process API Updates
- Java 11+ Features
* Local-Variable Type Inference (var keyword)
* Enhancements in Optional class
* New Methods in the String class relevant to concurrency
Such a crystal clear explanation of core concepts, I don't think no body explains so well even in highly paid courses.
Thanks Shreyansh!!
Thanks 🙏
those who sell high paid course are the ones who got laid off or quid due to lack of real knowledge
Bhai itna detail me self study karna ho toh?
The difference here is the blend of java concepts + core cs concepts. Very rares you find some one teaching a language actually mapping with its actual cs core concepts.
I just finished watching this video and I wanted to take a moment to commend you on the fantastic job you did.
Your clear explanations and engaging presentation made complex concepts easy to understand. The way you break down the information is amazing.
It's evident that you put a lot of effort into your content, and it truly shows.
Thank you for creating such valuable and insightful videos that make learning enjoyable!
You have the GIFT in Teaching !
what a lecture, what a learning, awesome, as a professional, i didn't knew 10% of what u taught today, kudos to you
thanks
How someone can give indept knowledge for free..I'm glade to have you as java trainer
happy learning
It's absolutely clear and such a great learning, I can't thank you enough for sharing such valuable knowledge
thanks
Great explanations and main thing is that you are teaching everything based on your real industry experiences. Great learning experience! Heartful Thanks to you!
God Bless You Brother😊. It was indepth and in one flow. We kind of learn in peaces and we forget, but tought us in peaces and also combined them in a single flow, which is more beautiful. You are a great god gifted teacher, carry on. Keep Sharing. Be Blessed and Be Blessing for others as you already are 😊
One small request though, the last part where the actual execution that happens on CPU was bit unclear to me, if possible could be please create a seperate video which explains that in details?
Thanks, sure will cover
I have been thinking my professor made me understood OS very clear during college....I was wrong the whole time
When I run
Java -Xms256m -Xmx2g Test
What will be the total JVM heap memory allocated.
How much each process JVM instance will get if I have 2 process running.
And how it will be decided?
Just Loved It ! 1 complete / all 10 Multithreading videos. This weekend I will complete all 10 !
Following java Playlist and explanation 👌.
When can we expect spring Playlist ?
After Java, Java thodi hi bachi hai bas
The Physical Memory in the diagram is RAM, right? Let’s say I have a 8gb RAM system and 600mb are free.
If I do -
java -Xms1g -Xmx2g Test
Will it give OutOfMemoryError ?
Hi Shrayansh,
Great, video once again but where is part 2?
Also Can you please brief me about how the metaspace came in picture and in which memory it will get allocated. from instance of JVM or the central JVM or The natice memory?
Eagerly waiting for the spring playlist
God-level concepts.Thanks for such valuable content.
thanks
you made my day.. Thanks for the crystal clear explanation in simple terms ❤🎉
I am struck 35:00 second where i am not able to understand who will start the main thread?
Teaching is good but... This unlocking vdos not giving so much link
Thank you for such a nice explanation!
how can i download the notes
Yeah even I am not able to download Zoho one
@Shrayansh bhai, Kindly enable download on member videos. Some of us have subscribed to membership, download & watch videos offline, especially I use my commute time to watch videos offline.
ack, will do buddy
Hi Shrayansh,
Can you please help with an answer?
Compilation (javac Test.java): generates bytecode that can be executed by JVM.
Execution (java Test): at this point, JVM starts the new Process, here Test is the class that has the "public static void main(String args])" method.
In your notes above, it is stated that JVM starts a process.
and later you said that once the process is created, a new JVM instance is created.
Are there two JVM instances, one which creates a process and the other one that is being created once the process is created?
Edited: When you run "java Program", OS launches a process called JVM process. JVM initialises resources like memory and stuff like loading byte code into memory. And then it looks for main thread in the code and starts running that thread. Yes, When you run a program, the JVM process is created and the threads in your program run inside it.
If coder dont create threads exclusively..task is completed by only main thread?
Right
Very nice explaination. Thank you Sir
You deserve more than million subs 👍👍👍👍👍
wow .just amazing
One of the Best Vedios of Explaining the working of Threads in Java, totally loved it, it also cleared my doubt regarding the CPU cores and the Thread allocation to them, how they work in parallel and with context switching. Thank You So Much and Kudos to You!!
thanks
maza aaya kaafi din baad kuch padh ke.
#Day4 of 7Days of java
Hi brother, i have bought membership but still unable to watch videos, there is note that says, you need membership, but i am already member, i bought it a day ago
kindly check the membership level pls
Worth to have your channel's membership. Nicely explained.
🙏
I believe even the creator of Java couldn't explain it better than you did.
faad diya
Nice investment by me on myself
🤘
22:15 shouldnt we call this Interpreted bytecode instead of compiled bytecode?
Btw I have faced this issue of writing junits for multithreading and also debugging issues. We used webclient for rest call and it was customized heavily, so a big chunk of code was not under any junits. It reduced our ovrall test coverage but mejority of use were not able to write test for it, not even the developer who wrote the class at the first place.
Can you please add one video to address these two challenges you mentione?
VIDEO 3
My Motive for commenting: Watching all videos of sir/bhaiya , to get his personal guidance and referals for sde job .
My Feeling : Amazing to learn from someone in big position in paypal company
Want to be under ur guidance and chatrachaaya
Thankyou Shrayansh sir/bhaiya
what happens for api call it create separates process for each api call and allocates heap memory.
Each api calls his handle by spring internal threads
A 19:11, how does it get decided that how much Total JVM Heap Memory will be allocated
Also, at 20:30, how much memory is allocated to the code segment, data segment, register, stack, and counter together?
Is it different from heap memory?
@Shreyansh Jain
Thank you for the informative lectures, recently took the membership, but not able to find multithreading notes link in member community post. Please help.
Bit a MIxture of Compiler , Os , Computer Architechture ,nice video
Hi very useful video, but I guess you didn't make a playlist for this. It's a little difficult to find the next part's video in this sequence.
Where is metaspace in this?
🙌
Truely very great content man!!
Just one suggestion, is it possible that when you research for these videos you can mark the sources too any books articles etc.
And for this thread series any suggestion for any good resources explaining with this level of deepness or any sources you used can you link them.
Thanks a lot for this wonderful knowledge sharing. Can someone please tell where is the remaining videos for multithreading part 2
Good videos. Cleared lot of doubts.
Thanks
Nice
@Concept_&&_Coding How does JVM know how many threads it requires to execute a program?
Does JIT create all the required threads before running a process? Refer to the video at 32:30. This seems counter intutive
Hi sir. Thanks for the clear explanation, just a doubt. What is the difference between JVM heap size and JVM instance heap size? Is there something like global JVM? Thanks
Why does it never happen that multiple threads execute the same statement multiple times how is it distributed among threads??
Thank you.
If there is one CPU and one register so how is it possible for thread having their individual registers
Wooo...Take a bow....
Such a great explanation..🙌
Really looking forward for upcoming ones for this topic.
Also, show the things with code execution wherever it is possible, which you always does so. Thank you!
Sure, thanks 🙏
It's amazing the details shared in the video, please let us know where do you get all the knowledge from, the books you read or any kind of other resources. It will be very helpful for self-study and upskilling.
thanks. Many with experience in would say Aditya. Its been 8yrs and still working with Java tech stack only
thanks shreyansh
why is thread the smallest sequence of instruction? What does smallest here refer to?
Hi Shreyansh why doesn't the below code work asproducer and consumer problem's solution import java.util.*;
class Producer
{
Queue queue;
Producer(Queue queue){
this.queue= queue;
}
public void produce(int data) throws Exception{
synchronized(queue){
while(queue.size() == 3){
//waiting for consumer to consume items
System.out.println("waiting for consumer to consume items");
wait();
}
System.out.println("Added item: " + data);
queue.add(data);
notify();
}
}
}
class Consumer
{
Queue queue;
Consumer(Queue queue){
this.queue= queue;
}
public void consume() throws Exception{
synchronized(queue){
while(queue.isEmpty()){
//waiting for producer to add items into queue
System.out.println("waiting for producer to add items into queue");
wait();
}
System.out.println("Removed: " + queue.poll() + " Current Queue Size is: " + queue.size());
notify();
}
}
}
public class Main{
public static void main(String[] args) throws Exception{
Queue sharedQueue= new PriorityQueue(3);
Producer producer= new Producer(sharedQueue);
Consumer consumer= new Consumer(sharedQueue);
Thread producerThread= new Thread(() -> {
try{
for(int i= 0; i {
try{
for(int i= 0; i
ack will check buddy
context swithing is b/w processes or b/w thread or both ? (47:08)
Sir i want your suggestion i not able to understand the reactjs and backend technologies 😢 and i am not able to improve my problem solving skills i am in third year loosing interest in studies can u tell me how to get back to track
you are doing a great job. Keep it up
one small thing that took me to other articles was what register is storing .. seems like its saving the operator , operands. there are multiple registers from what i have come to understand .. i guess atleast 3 to store operator , operators and accumulator. to come back to do what it was doing. Simply great work though . This must be taking so much of your time! good job again
What algorithm does this context switching follow? FIFO? Shortest job? Round robin?
its depends on scheduler to scheduler, various algo like priority based scheduling and there is one more time-sharing scheduling algo is used.
but i need to check more on that.
Nice Explanation...please create videos on Aspect Oriented Programming, I've got several question from AOP and spring AOP in my recent interviews
ack
Hey Shreyansh, Any idea about how virtual threads are working ? Is Virtual thread same as a Thread ? if possible, Can you come up with some sort of video which will explain Virtual Thread ?
Hi virtual thread is not same as Thread but they are backward compatible.
In our traditional thread, JVM create respective native thread (OS level Kernel thread). so you can say JVM provide us the abstraction, when we say Thread.start(), it actually tells OS to create new native thread.
But Virtual thread, introduced in Java19, here JVM takes the responsibility of managing the threads and it creates many virtual thread and map to very less number of native thread. Means Multiple virtual thread can work on single virtual thread.
Virtual thread provides backward compatability, i can create a video of it. But i need to install latest JDK for it :) will do it later and create a video. But i know this info will help you in clearing your doubt.
@Shreyansh Great content. One thing I wanted to know is where is Code Segment present? In the ROM/EPROM? What about extra segment ? In the RAM? This needs to be known. Probably a small wrapper class on CPU architecture and where different segments are located shall help understand multithreading better
Really nice lecture, you made my day. I was literally searching for a Java course online and your course series is truly awesome. Thanks for your effort; such high-quality resources are rarely found almost free anywhere else.
Thanks, Shreyansh ❣
Glad to hear that
brilliant explanation, thoroughly enjoyed it...where can i find part 2 and so on?
check the playlist 30th no video is part 2
@Shreyansh Great Explained but where is part 2 video?
already uploaded buddy 7-8 parts are there all uploaded
Is data segment alias for metaspace?
Who is bro?💀🙌
One of the best 👍
Thanks
Bro, unable to find part 2, is that uploaded/created?
video no 30. is part2
If I don't specify the heap memory size at the time of process execution, how much heap memory will be allocated to this process then?
Default values are used which depends on system configuration and JVM.
Generally min geap is 1/64 of total RAM.
And Max heap is 1/4.
But these values can change depending upon the system.
There is a command too, through which you can know how much default heap value is set. I will look for that command and share with you.
@@ConceptandCoding thank you for your response . Please do let me know the command.
Really liking your explanation.
1 doubt I had. Where exactly we tell how much heap memory to be allocated while creating a process?
sorry got it. at the time of process execution itself we specify memory. e.g
java .
However, if I don't specify the heap memory size at the time of process execution, how much heap memory will be allocated to this process then?
@@gauravraj2604 yes
@@ConceptandCoding However, if I don't specify the heap memory size at the time of process execution, how much heap memory will be allocated to this process then?
Please make video on lambda functions also.
It's already there, pls check previous videos in Java playlist buddy
Point to point brings very much clearity. Thank You
thanks
Please include coding also bhai..
I will, this is fundamental video, what is process and thread. Thread creation will go next with coding buddy