Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 330 -.NET Applications1 Chapter 8 – Multithreading and Concurrency Management.

Similar presentations


Presentation on theme: "CIS 330 -.NET Applications1 Chapter 8 – Multithreading and Concurrency Management."— Presentation transcript:

1 CIS 330 -.NET Applications1 Chapter 8 – Multithreading and Concurrency Management

2 CIS 330 -.NET Applications2 Objectives Threads.Net Thread Class Thread Blocking Synchronizing Threads.Net Synchronizing Options

3 CIS 330 -.NET Applications3 Threads Thread: path of execution within a process Every application runs on at least one thread Applications use Two kinds of operations: –CPU-bound –I/O-bound CPU-bound operation threads more advantageous on multi- processor machines I/O-bound operation threads often ran concurrently with CPU-bound ones Multithreading: Can improve Application throughput and performance Most User Interfaces run on separate threads

4 CIS 330 -.NET Applications4 Threads (cont) Each Thread is allocated –registers –program counter –a stack and a stack pointer –is assigned a time slot and a priority Operating System manages –thread scheduling –context switches –thread-manipulation requests (start, sleep, etc.)

5 CIS 330 -.NET Applications5.NET Thread Class Defined in System.Threading namespace Some Properties: –CurrentThread : static property that returns an instance of the current managed thread –ManagedThreadId : returns unique thread id –Name : string property that can be set and used by developers

6 CIS 330 -.NET Applications6 Spinning off a new thread using System.Threading; Public class MyClass { Thread currentThread = Thread.CurrentThread; string caption = “Thread ID = “; caption += currentThread.ManagedThreadId; MessageBox.Show(“ShowMessage runs on new thread”,caption); } MyClass obj = new MyClass(); Thread workerThread = new Thread(obj.ShowMessage); workerThread.Start();

7 CIS 330 -.NET Applications7 Thread Blocking Suspend(); Resume(); Sleep(int millisecondsTimeout); Sleep(TimeSpan timeout); SpinWait(int iterations); Join(); overloaded with int or TimeSpan parameters Interrupt(); Abort(); None of the above are really recommended – recommend use of.NET synchronization objects Race Condition: when one thread must wait for another thread to complete before proceeding

8 CIS 330 -.NET Applications8 Threads (cont) Threads know their state through the ThreadState enumerated type property (Aborted, Running, Stopped, etc.) Foreground and Background Threads –When all foreground thread processes have exited, the application closes Thread Priority and Scheduling –Should mess with priorities –Scheduling is preemptive

9 CIS 330 -.NET Applications9 Synchronizing Threads In multithreaded applications you must synchronize access to objects by concurrent multiple threads All components must be thread-safe: uses mechanisms to prevent multiple threads from simultaneously accessing its methods and corrupting its state. Deadlock: when two threads simultaneously attempt to access each other’s tread-safe resource Writing robust, high-performance multithreaded code requires a great deal of skill and discipline

10 CIS 330 -.NET Applications10 Synchronization with.NET Automatic Synchronization –Use the [Synchronization] attribute for whole components –Based on the.NET architecture that organized components within separate Processes, App Domains, Contexts, and Synchronization domains. See page 204 and 206. –Easy to use Manual Synchronization –Uses rich set of synchronization locks (monitors, events, mutexes, semaphores, and interlocks) –Fine-grained control over what is locked (objects, members, or single line of code) –Requires careful design to limit deadlocks, state corruption, and race conditions

11 CIS 330 -.NET Applications11 Summary Threads.Net Thread Class Thread Blocking Synchronizing Threads.Net Synchronizing Options


Download ppt "CIS 330 -.NET Applications1 Chapter 8 – Multithreading and Concurrency Management."

Similar presentations


Ads by Google