Threading using C# and .Net

Slides:



Advertisements
Similar presentations
How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.
Advertisements

Multithreading. RHS – SWC 2 What is a thread Inside a single process, multiple threads can be executing concurrently A thread is the execution of (part.
Chapter 14 Multithreading Yingcai Xiao. Multithreading is a mechanism for performing two or more tasks concurrently.  In the managed world of the common.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Multithreading.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Definitions Process – An executing program
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Multithreading.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
Windows Programming Using C# Threading. 2 Contents Threading Thread class Interlocked class Monitor class Semaphore class Thread Pools.
CNS 3260 C# .NET Software Development
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
111 © 2002, Cisco Systems, Inc. All rights reserved.
C# I 1 CSC 298 Threads. C# I 2 Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The execution.
CSE 501N Fall ‘09 23: Advanced Multithreading: Synchronization and Thread-Safety December 1, 2009 Nick Leidenfrost.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
Upcoming Presentations ILM Professional Service – Proprietary and Confidential ( DateTimeTopicPresenter March PM Distributed.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
SurfaceView.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 More on Thread API.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
CIS NET Applications1 Chapter 8 – Multithreading and Concurrency Management.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Threads and Thread Synchronization. Introduction In windows the basic unit of execution is the thread. It is the smallest schedulable unit of execution.
Multithreading / Concurrency
Threading Lecture 11 M. Ipalakova.
Multithreading.
Background on the need for Synchronization
PA1 Discussion.
Applied Operating System Concepts -
Jim Fawcett CSE681 – Software Modeling & Analysis Fall 2002
Multithreaded Programming in Java
Critical sections, locking, monitors, etc.
5-High-Performance Embedded Systems using Concurrent Process (cont.)
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
ITEC324 Principle of CS III
Multithreading.
Java Based Techhnology
Multithreading.
Multithreaded Programming
Threads Chapter 4.
Java Concurrency 17-Jan-19.
Concurrency: Mutual Exclusion and Process Synchronization
Conditions for Deadlock
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
Java Concurrency.
Multithreading in java.
CS333 Intro to Operating Systems
Java Concurrency.
Threads and Multithreading
Foundations and Definitions
Multithreading Dr. John P. Abraham.
Java Concurrency 29-May-19.
Visual Programming COMP-315
CSE 542: Operating Systems
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Threading using C# and .Net Presented by: Gurpreet Singh Assistant Professor Department of School of Computing and Engineering Galgotias University

Outline : Threads System.Threading Namespace . Thread Class – its methods and properties. Thread Synchronization. Monitors C# Lock keyword. Reader/Writer Locks Conclusion

Threads : Thread is the fundamental unit of execution. More than one thread can be executing code inside the same process (application). On a single-processor machine, the operating system is switching rapidly between the threads, giving the appearance of simultaneous execution.

With threads you can : Maintain a responsive user interface while background tasks are executing Distinguish tasks of varying priority Perform operations that consume a large amount of time without stopping the rest of the application

System.Threading Namespace Provides classes and interfaces that enable multithreaded programming. Consists of classes for synchronizing thread activities . Chief among the namespace members is Thread class

Thread Class Implements various methods & properties that allows to manipulate concurrently running threads. Some of them are : CurrentThread IsAlive IsBackground Name Priority ThreadState

Starting a thread : Thread thread = new Thread(new ThreadStart (ThreadFunc)); //Creates a thread object // ThreadStart identifies the method that the thread executes when it //starts thread.Start(); //starts the thread running Thread Priorities : Controls the amount of CPU time that can be allotted to a thread. ThreadPriority.Highest ThreadPriority.AboveNormal ThreadPriority.Normal ThreadPriority.BelowNormal ThreadPriority.Lowest The parameter of the Thread class is reference to a ThreadStart class. ThreadStart class points to the method that should be executed first when a thread is started.

Suspending and Resuming Threads Thread.Suspend temporarily suspends a running thread. Thread.Resume will get it running again Sleep : A thread can suspend itself by calling Sleep. Difference between Sleep and Suspend - A thread can call sleep only on itself. -Any thread can call Suspend on another thread.

Terminating a thread Thread.Abort() terminates a running thread. In order to end the thread , Abort() throws a ThreadAbortException. Suppose a thread using SQL Connection ends prematurely , we can close the the SQL connection by placing it in the finally block. - SqlConnection conn ……… try{ conn.open(); …. ..... } finally{ conn.close();//this gets executed first before the thread ends.

catch(ThreadAbortException){ Thread.ResetAbort(); A thread can prevent itself from being terminated with Thread.ResetAbort. - try{ … } catch(ThreadAbortException){ Thread.ResetAbort(); Thread.Join() When one thread terminates another, wait for the other thread to end.

Thread Synchronization : Threads must be coordinated to prevent data corruption. Monitors Monitors allow us to obtain a lock on a particular object and use that lock to restrict access to critical section of code. While a thread owns a lock for an object, no other thread can acquire that lock. Monitor.Enter(object) claims the lock but blocks if another thread already owns it. Monitor.Exit(object) releases the lock.

Monitor.Enter(buffer); try critical section; } finally Void Method1() { …. Monitor.Enter(buffer); try critical section; } finally Monitor.Exit(buffer); Calls to Exit are enclosed in finally blocks to ensure that they’re executed even when an exception arises. Calls to Exit are enclosed in finally blocks to ensure that they’re executed even when an exception arises. Otherwise it may lead a thread to acquire lock indefinitely and causes other threads to hang.

Monitor.Enter(buffer); try { critical section; finally The C # Lock Keyword : lock(buffer){ ……. } is equivalent to Monitor.Enter(buffer); try { critical section; finally Monitor.Exit(buffer); - Makes the code concise. - Also ensures the presence of a finally block to make sure the lock is released. Finally block is there but can’t be seen. It is automatically generated in the CIL statements.

Prevent concurrent threads from accessing a resource simultaneously. Reader/Writer locks : Prevent concurrent threads from accessing a resource simultaneously. Permit multiple threads to read concurrently. Prevent overlapping reads and writes as well as overlapping writes. Reader function uses : -AcquireReaderLock -ReleaseReaderLock Writer funciotn uses : ReleaseLocks are enclosed in finally blocks to be absolutely certain that they are executed AcquireReaderLock blocks if the lock is currently owned by a writer thread but not if it’s owned by other reader threads. AcquireWriterLock blocks if the lock is owned by any one. Consequently multiple threads can read the resource concurrently , but only one thread at a time can write to it and it can’t wait if another thread is reading.

Drawback : Threads that need writer locks while they hold reader locks will result in deadlocks. Solution is UpgradeToWriterLock and DowngradeFromWriterLock methods.

Conclusion : Using more than one thread, is the most powerful technique available to increase responsiveness to the user and process the data necessary to get the job done at almost the same time.

References : Programming Microsoft .NET – Jeff Prosise http://msdn2.microsoft.com/ http://cs193n.stanford.edu/handouts/pdf/37%20 Streams,%20Multithreading.pdf