End Sub. 21. Build and run the application.
21. Build and run the application. 22. You should observe the employees are processed in order of their IDs. Also note the total amount of time required to walk the tree (the exact time required will vary): Figure 5 Output from a non-parallel tree walker
Note: The tree has been structured so that the data will be written out in ID order when the tree is walked using the non-parallel algorithm provided above.
23. To walk the tree in a parallel manner remove the two calls to WalkTree() at the end of the WalkTree() method and replace them with a call to the Invoke() Method of the static Parallel class: C# private static void WalkTree(Tree<Employee> node) { if (node == null) return;
if (node.Data!= null) { Employee emp = node.Data; Console.WriteLine("Starting process for employee id {0}", emp.EmployeeID); decimal span = PayrollServices.GetPayrollDeduction(emp); Console.WriteLine("Completed process for employee id {0}", emp.EmployeeID); Console.WriteLine(); }
Parallel.Invoke(delegate { WalkTree(node.Left); }, delegate { WalkTree(node.Right); }); }
Visual Basic Private Sub WalkTree(ByVal node As Tree(Of Employee)) If node Is Nothing Then Return End If
If node.Data IsNot Nothing Then Dim emp As Employee = node.Data Console.WriteLine("Starting process for employee id {0}", emp.EmployeeID) Dim span As Decimal = PayrollServices.GetPayrollDeduction(emp) Console.WriteLine("Completed process for employee id {0}", emp.EmployeeID) Console.WriteLine() End If
|