Objective 1.1: Implement multithreading and asynchronous processing Flashcards
What is the problem with single-threaded applications?
Long running operations and unrecoverable errors can freeze the rest of the machine.
What is parallelism?
The use of multiple threads in an application so as to take advantage of multi core systems and stay responsive.
Involves taking a certain task and splitting it into a set of related tasks that can be executed concurrently.
What is a process?
Isolates an application from other applications by giving it its own virtual memory.
What is a thread?
Is like a virtualized CPU.
Each process run in its own thread.
What is context switching?
Windows executes each thread for a certain period of time, after which the thread is paused and a new thread is switched in.
What is the disadvantage of context switching?
There is a performance overhead as each thread uses a certain area of memory, CPU registers and other state data, and Windows must ensure that the whole context is saved and restored on each switch.
What is the Thread class?
Allows to create new threads, manage their priority, get their status and various other advanced options.
However it shouldn’t be used unless absolutely necessary.
What is synchronization?
Mechanism of ensuring that two threads don’t execute a specific portion of the program at the same time.
What is Thread.Join used for?
It is invoked on a Thread instance and within another thread, e.g., the main thread. This will force the main thread to wait for the other thread to finish.
What is the default priority of a new thread?
Normal.
What is the use of Thread.Sleep(0)?
Signals Windows that the thread is finished, so that it can immediately switch to another thread, instead of waiting for the whole time slice of the thread to finish.
What is the difference between foreground and background threads?
Foreground threads can be used to keep an application alive.
Only when all foreground threads end will the CLR shut down your application.
Background threads are then terminated.
What is the ParameterizedThreadStart class used for?
An instance of this, when passed into the Thread constructor, is used when you want to pass some data through the start method of your thread to your worker method.
Data is passed as an object and will need to be parsed to be usable.
What is the Thread.Abort method used for?
Used to stop a thread, at which time a ThreadAbortException is thrown on the target thread.
It is recommended to avoid this and use a shared variable instead for stopping a thread.
What is a lambda expression?
Short hand version of a delegate.
What is the purpose of the ThreadStatic attribute?
When a field is marked with this, it allows each thread to get its own copy of it.
What is the purpose of the ThreadLocal class?
Class that takes a delegate to a method that initializes a value, in the event that you want to use local data in a thread and initialize it for each thread.
What is the purpose of the Thread.CurrentThread class?
Gives execution context of the thread that is currently executing.
What information exists in the execution context?
Current culture is a CultureInfo instance associated with the current thread that is used to format dates, times, numbers, currency values, the sorting order of text, casing conventions, and string comparisons.
Principal represents the current security context.
Priority indicates how the thread should be scheduled with the OS.
Etc
What is the purpose of the ExecutionContext.SuppressFlow method?
Prevents the initiating threads execution context to be flowed to the new thread, as the copying of data could cost some resources.
What is a thread pool?
Created to reuse threads as the creation and cleanup of new threads costs time and resources.
In .NET you queue a work item which is then picked up by an available thread.
What are the pros and cons of thread pools?
Manages the creation and disposal of threads as needed, i.e., starts out empty and can be empty after a long time.
Local state is reused since threads are reused.
Lesser degree of parallelism so server can become unresponsive, but helps prevent server crash.
What is a Task?
Object that represents some work that should be done. Unlike Threads, it can tell you if the work is completed, and it’s return result (if any).
Recommended way to create multithreaded applications.
What is a Task Scheduler?
Responsible for starting a task and managing it, and by default does so by using threads from the thread pool.
What is the purpose of Task.Wait?
Waits for the task to complete before exiting the application, much like Thread.Join.