week 2 - Introduction to multithreading in java Flashcards
(3 cards)
threads
threads:
a unit of execution thats responsible for handling a specific subtask for the process.
Threads share the same address space as the process that created them and the same heap but they do not share STACKS , registers and program counter
2 ways to create threads
so essentially two ways to create threads
one we extend the thread class and we override the run method to achieve thread functionality
note : with this approach of thread calss new object created each time so no shared attributes -> cant lead to race conditions
another way to create threads is to
implement runnable interface
forced to override the run method now
we call the thread constructor and we can pass that implements runnable as a parameter to thread constructor
This is succeptible to race condidtions as private attributes will be shared so if we have multiple threads executing (reading and accessing) it will lead to lost updates due to unpredicatable execution order
just read
Threads dont obey execution of order in program
due to premptive schedule - as processes get interrupted and os decideds what to schedule next