Пользовательский элемент управления. Создание клиентского приложения. Развитие серверного проекта, страница 13

The operation system decides how to allocate CPU’s time to the processes according to the process’s priority level. In Windows 2000 there are four priority levels, which can be assigned to processes:

¨  Real time – highest priority. The processes with real time priority can interrupt all other processes with lower priority. This type of priority is reserved for processes which must not be interrupted in order to be done well like streaming video or applications with complex graphics.

¨  High priority: for processes that should be executed immediately. Such as Windows Task List, which should be fired immediately after the user request. The high priority processes can be interrupted only by processes with real time priority and can preempt all processes with lower priority.

¨  Normal priority: for normal applications, which do not have any special CPU’s time allocations.

¨  Idle priority: for processes, which are done only when the CPU is idle, such as Screen saver.

The above-mentioned priority levels exist in Windows2000 and in .NET they are different but the logic behind them is the same. The priority levels in .NET will be discussed later in this article. (Aitken, Peter; 2002)

Types of Threading in Windows

There are three basic types of threading in Win32 environment:

¨  Single Threading – There is only one thread in the application, and it has to do all the work. That thread has all the space allocated for the process.

¨  Apartment Threading – There are multiple threads within the application. The application defines when and for how long a thread should be executed. Each thread has an assigned space within the space allocated for the application and the threads do not shared that resource.

¨  Free Threading – There are multiple threads within an application and these threads do share resources among. Different threads can call the same method and property at the same time.

The apartment model is more efficient than the single threading because the work is divided among multiple objects, while the free model is the fastest and the most efficient. But the free model hides many risks in the same time. Because of the sharing of resources among multiple objects, much more attention must be paid over synchronizing that activities and not allowing mutual exclusive changes to happen. (Ewing, Greg; 2002)

Single threaded versus Multi threaded application

For example if the application consists of only one thread and there is an operation which can take much time such as requesting data from a remote server. The CPU will send the request to the server and wait for the response. The request will travel to the server, which let’s say is in Australia. The server will receive the request will prepare the data and send them back. For all this time while the request and the data travel the application will and after that will fire any other operation. This can result in much idle time and the user will have to wait without doing anything else.

On the other hand if the application uses more than one thread, the operation of requesting data from a remote computer can fire a new thread, and the running one will still be on focus, allowing the user to perform what action wants, while the request and data travel.

Windows is multi threaded because we can download a file from Internet and in the same time write a document using Ms Word and listen to a music CD. What the operation system does is dividing the CPU’s time to small time spans for each one of these processes. Because the CPU is very fast and the time spans are really small everything seems to done simultaneously.

Let’s assume again that we have two identical applications but one of them consists of one thread and the other one has many threads. Let’s also assume that the application employs operations, which leave the CPU idle for a relatively long time such as connecting to a remote computer, requesting data from a remote server etc. If we subtract the time for which the CPU has been idle from the total time the application has run, we will receive the time the CPU has worked for the one threaded application. But for the multi-threaded application we can find the total time the CPU has worked by adding the time the CPU has worked over each thread. If we compare both times we will find that the CPU has been more efficiently used by the multi-threaded application.