What should you do?

You are currently in the process of developing a business logic component that requires long calculations.
You have identified numerous tasks within this application that can be done asynchronously.
You notice that these tasks are mutually dependent and require complex synchronization techniques so that it can manage efficiently.
You decide to use Microsoft .NET 2.0 to take advantage of its new thread management features.
You need to create and start the application threads.
What should you do?

You are currently in the process of developing a business logic component that requires long calculations.
You have identified numerous tasks within this application that can be done asynchronously.
You notice that these tasks are mutually dependent and require complex synchronization techniques so that it can manage efficiently.
You decide to use Microsoft .NET 2.0 to take advantage of its new thread management features.
You need to create and start the application threads.
What should you do?

A.
Use the following code:
ThreadPool thPool = new ThreadPool(“Current Application”);
Thread th1 = new Thread(Task1);
Thread th2 = new Thread(Task2);
Thread th3 = new Thread(Task3);
th1.StartInPool(thPool);
th2.StartInPool(thPool);
th3.StartInPool(thPool);

B.
Use the following code:
ThreadPool thPool = new ThreadPool(“Current Application”);
thPool.QueueUserWorkItem(Task1);
thPool.QueueUserWorkItem(Task2);
thPool.QueueUserWorkItem(Task3);

C.
Use the following code:
ThreadPool.QueueUserWorkItem(Task1);
ThreadPool.QueueUserWorkItem(Task2);
ThreadPool.QueueUserWorkItem(Task3);

D.
Use the following code:
Thread th1 = new Thread(Task1);
Thread th1 = new Thread(Task2);
Thread th1 = new Thread(Task3);
th1.Start();
th2.Start();
th3.Start();

Explanation:
This code uses the QueueUserWorkItem method of the ThreadPool class to add tasks to the current application domain’s thread pool. The QueueUserWorkItem method takes a WaitCallback delegate as an argument and manages the tasks using background threads. This allows the developer to concentrate on business logic and requires minimal synchronization code.
Incorrect Answers:
A D: You should not use either of the code fragments that instantiate the Thread objects explicitly because it will require excessive synchronization code to manage effectively.
B: You should not use the code that instantiates a ThreadPool object because the ThreadPool class is a static class and cannot be instantiated.
public static class ThreadPool
ThreadPool ist eine statische Klasse !!! – daher kein Instanz m�glich !!!
ThreadPool.QueueUserWorkItem – �bernimmt das starten selbst (wenn aus der Warteschlange !!!)
CallBack (WaitCallback) ist m�glich.



Leave a Reply 1

Your email address will not be published. Required fields are marked *


mr_tienvu

mr_tienvu

I agree with the answer. C