Содержание
- 2. Outline : Threads System.Threading Namespace . Thread Class – its methods and properties. Thread Synchronization. Monitors
- 3. Threads : Thread is the fundamental unit of execution. More than one thread can be executing
- 4. With threads you can : Maintain a responsive user interface while background tasks are executing Distinguish
- 5. System.Threading Namespace Provides classes and interfaces that enable multithreaded programming. Consists of classes for synchronizing thread
- 6. Thread Class Implements various methods & properties that allows to manipulate concurrently running threads. Some of
- 7. Starting a thread : Thread thread = new Thread(new ThreadStart (ThreadFunc)); //Creates a thread object //
- 8. Suspending and Resuming Threads Thread.Suspend temporarily suspends a running thread. Thread.Resume will get it running again
- 9. Terminating a thread Thread.Abort() terminates a running thread. In order to end the thread , Abort()
- 10. A thread can prevent itself from being terminated with Thread.ResetAbort. - try{ … } catch(ThreadAbortException){ Thread.ResetAbort();
- 11. Thread Synchronization : Threads must be coordinated to prevent data corruption. Monitors Monitors allow us to
- 12. Void Method1() { …. Monitor.Enter(buffer); try { critical section; } finally { Monitor.Exit(buffer); } } Calls
- 13. The C # Lock Keyword : lock(buffer){ ……. } is equivalent to Monitor.Enter(buffer); try { critical
- 14. Reader/Writer locks : Prevent concurrent threads from accessing a resource simultaneously. Permit multiple threads to read
- 15. Drawback : Threads that need writer locks while they hold reader locks will result in deadlocks.
- 16. MethodImpl Attribute For synchronizing access to entire methods. To prevent a method from be executed by
- 17. Conclusion : Using more than one thread, is the most powerful technique available to increase responsiveness
- 19. Скачать презентацию