Introduction to LINQLINQ stands for L anguage- In tegrated Q uery, it is a query syntax used to bridges the gap between the world of objects and the world of data.. LINQ to Objects LIN
Trang 7Introduction to LINQ
LINQ stands for L anguage- In tegrated Q uery, it is a query syntax used to bridges the
gap between the world of objects and the world of data.
LINQ to Objects LINQ to DataSet LINQ to XML LINQ to Entities
LINQ to SQL
Method Syntax Query Syntax
Trang 9Introduction to LINQ
Familiar language : you don’t have to learn a new query language for each type of data source or data format.
Less coding : It reduces the amount of code comparing to the normal approach.
Readable code : LINQ makes the code more readable
Standardization : The same LINQ syntax can be used to query multiple data sources.
Trang 12LINQ - method syntax exercise
Salary about higher than 4000 and last appraisal less than 8
Use the DisplayWithAppraisal method
Trang 15LINQ - query syntax exercise
Salary about higher than 4000 and last appraisal less than 8
Use the DisplayWithAppraisal method
Trang 21keystrokes Checking Syntax Displaying Intellisense Offering Solutions for errors
Trang 22Introduction to multithreading
Advantages of multithreading
Responsiveness: multithreading allow an application to remain responsive.
Faster execution: multithreaded applications operate faster on computers that have multiple CPUs.
Lower resource consumption: multithreaded applications can handle multiple requests simultaneously using fewer resources Better system utilization : multithreaded applications can be doing different tasks at the same time not in a sequential order.
Disadvantages of multithreading
Complexity: Increases the complexity of your application
Difficulty to write code: because you are place each task on a separate independent thread
Difficulty to debug code: because the application will not work in a sequential way anymore
Difficulty to test code: for the same previous two reasons
Potential deadlocks : when two or more threads are blocking each other.
Trang 23Introduction to multithreading
Critical Section
is a section of code that needs to be executed without being interpreted.
For example
• A user trying to reserve the last ticket available on a plane
• One thread is opening a file and another thread is writing in the file
Trang 31Deadlocks and lock keyword
Deadlock occurs when a thread enters a waiting state because a requested system resource is held by another waiting thread, which in turn is waiting for another resource held by another waiting threat.
Trang 32Deadlocks and lock keyword
Lock keyword will ensure that one thread is executing a piece of code at one time Which means
that one thread does not enter a critical section of code while another thread is in that critical
section.
Trang 37Limits the number of threads that can access a resource or pool of resources concurrently.
Trang 39Introduction to asynchronous programming
Trang 40Introduction to asynchronous programming
Synchronous Programming
Single Threaded Multi-Threaded
Trang 41Introduction to asynchronous programming
Asynchronous Programming
Single Threaded Multi-Threaded
Thread 1
executing another tasks.
Thread 1
Thread 2
Thread 3
Task 2 Task 4
Trang 42Synchronous model means two or more tasks are running at the same time and it is possible
that one may block another
Asynchronous model means two or more operations are running in different contexts (thread)
so that they can run concurrently and do not block each other.
Trang 44Task represents an asynchronous operation.
Trang 56Assignment No.19: (Search in a list using LINQ)
• Display count of search result
• Add numbers before each line
• Add a parameter in the display method to display the title or not
Trang 57Assignment No.20: (Say hi in 7 different languages)
• Create a list of threads
• Create a class called Hi contains 7 or so different SayHi methods in different languages
• Each SayHi method has a name and a count to display how many times
Trang 58Assignment No.21: (Fastest task)
• Creating two tasks whatever task finishes first cancels the other one and display the maximum value the cancelled one reaches
Trang 59Assignment No.22: (ConcurrentStack)
• Create concurrent stack and fill it with integers using two different threads
• Access it using two different threads
• Count how many items each thread accessed