This workshop is a practical application of various techniques related to parallel programming in C#. The main objective is to provide hands-on experience with the concepts and tools necessary to solve problems related to parallel programming.
After completing this workshop, you will be able to:
- Manipulate a
Parallel.For
- Manipulate a
Parallel.ForEach
- Manipulate a
Parallel.Invoke
- Manipulate Tasks
- Manipulate an event
- Manipulate a delegate multi-cast
All exercises are performed in console-type applications. This workshop is designed to enhance your technical skills in this module. However, consistent work and rigorous implementation are essential for successful completion.
This workshop is expected to take approximately 4 hours.
Observe the provided code in the Program.cs
file. This code demonstrates the use of delegates, events, and parallel programming constructs.
Understand the philosophy of the provided code. What is it trying to achieve and how does it use the concepts of parallel programming to do so?
Translate the provided code line by line. Understand what each line of code does and how it contributes to the overall functionality of the program.
Write a simple program that demonstrates the use of Parallel.For
. This construct is used to execute a for
loop in parallel, which can significantly improve performance for CPU-bound tasks.
In this code, Parallel.For is used to execute a for loop in parallel. The loop runs from 0 to 9,
and for each iteration,it calculates the square of the current number and prints it. The order
of the output may vary because the iterations are running in parallel.
Write a simple program that demonstrates the use of Parallel.ForEach
. This construct is used to execute a foreach
loop in parallel, which can significantly improve performance when processing collections of data.
The code you provided is a good example of using Parallel.ForEach to improve performance when
processing large collections of data. It calculates prime numbers in a list using both a
regular foreach loop and Parallel.ForEach, and compares the time taken for each method.
Write a simple program that demonstrates the use of Parallel.Invoke
. This construct is used to execute multiple methods in parallel.
In this code, Parallel.Invoke is used to execute three methods in parallel: BasicAction,
a lambda expression, and an inline delegate. Each method prints a message to the console
along with the ID of the thread it's running on
Write a simple program that demonstrates the use of Task.Factory
. This construct is used to start and manage tasks.
This code is a simple C# program that demonstrates the use of Task.Factory.StartNew.
This method is used to create and start a new Task in one operation.
in this code, an array of Task objects is created. Then, a for loop is used to create
and start a new Task for each element in the array. Each Task prints a message to the
console indicating that it has started, sleeps for a period of time proportional
to its index in the array (simulating work), and then prints a message indicating that it has completed.