Microsoft Interview Question
Software Engineer / DevelopersThread execute several tasks simultaneously. They share resources of the process they belong to. Most of the modern applications make use of threads to get the job done quickly. Sometimes, bad practices of multithreading may cause deadlock. To avoid that, one must be careful writing multi-threaded applications.
Threads are sub-processes that execute independently but share the same memory of the parent process. Multithreading is useful to improve performance, by paralyzing tasks that are not dependent on each other and can run concurrently and your program will still work correctly. Problems that can arise are: data inconsistency if memory access by different threads is not handled properly: two threads writing to the same piece of memory or one writing and other reading at the same time with no lock protection. Another problem are deadlocks: no thread can proceed, because each thread is waiting for a resource that is being held by another thread, in a thread dependency cycle. Finally, if you use lock for everything the code will not take advantage of multi threading, the execution will seem sequential and the locks will add time to it.
thread interference: you dont want two threads to interleave while accessing shared variables .. inconsistencies arise
- AV September 24, 2008deadlock: don't want to be stuck in a situation where each of the thread is waiting for the other thread to release a lock .. none of the thread can make any progress ..
starvation: don't want a thread to starve for resources being consumed by the other thread .. long and 'greedy' synchronized methods lead to this ..
livelovk: pretty much like deadlock but the threads are not blocked waiting for each other but they are continuously responding to each other requests ... but none of them is able to make any progress ...