Friday, October 29, 2010

Threading

Threading enables concurrent processing, you can perform multiple tasks simultaneously.

Multithreading is the ability of operating system to concurrently run programs or tasks in separate threads.
Multithreading requires OS those can perform it, and we written programs in such manner that uses this ability of OS.
It’s a fast computation because it runs multiple tasks simultaneously in different threads.
Basically it’s a game of operating system, tasks not run simultaneously os allots time slots to each thread and runs threads for those allotted time slots, user response is very slow compare to system so user feels like processes running concurrently.
How threading works
Application domains run in processes, and each application domain starts with a single thread and then a thread can call other threads. Operating system divided processor time among threads, it allotted time slots to threads to be executed slots depends on priority, os and thread size.
When a thread switches from running state to suspended os saves context (information) for it, and loaded context of thread to be executed.
Thread pool class – To manage group of threads.
Timer - Enable calling to delegates after a certain amount of time.
Mutex - Mutex class is used to protect simultaneous access of a shared resource. State of Mutex object is set to 'signaled' when it is not owned by any thread or 'nonsignaled' when owned by any thread, so treads can own it one by one.

Pausing and resuming threads
Method Thread.Sleep(miliseconds) immediately suspend a thread for given milliseconds and other thread uses this time slice.
Thread.Sleep(Timeout.Infinite) - Sends to sleep for infinite time.
Thread.Abort() - Aborted the thread.
Thread.Interrupt() - Interrupt the thread.

Overview
1. Threads enables concurrent processing.
2. System.Threading provides classes and interfaces for it, ex to create, start, abort, suspend, synchronization of multiple threads etc.
3. Threads share application resources.

Using System.Threading;

Thread thrd = new Thread(new ThreadStart(obj.Method));
thrd.start();


No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...