Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Peer Thread Model

c



+ Font mai mare | - Font mai mic



Peer Thread Model

The next threading model we will describe is the Peer threading model. In this threading model, each thread will receive its own input from the appropriate sources and process that input accordingly. This model is illustrated in Figure 4.




Figure 4

In the figure above, the UI thread will receive the input from the keyboard and mouse and it can work accordingly. Worker Thread A will listen to a particular socket and process input as it comes in from that socket, and in the same way Worker Thread B will wait for a system event and act accordingly. In this model, all the threads execute concurrently without blocking or waiting for other threads.

We can amend the previous example so that the CalculateFactors() method notices when the factorial thread finishes, and discovers the factors of this number. We will use the factorial of 8 in this example. In this example, however, we won't be using a socket, but just the setting of a variable. The principles will be the same for sockets; you would either continuously listen, or to save processor cycles, sleep intermittently.

So, let's change the WorkerThread class first:

public class PeerThread

factors.Add(number.ToString());
return factors;
}
}

public ArrayList CalculateFactors()

ArrayList returnValue = CalculateFactors(factorial);
return returnValue;


public long CalculateFactorial(int number)

First, we'll explain the small changes. A private field has been created that will store the result of the factorial when it has been calculated. In addition, the class has been renamed to PeerThread. The CalculateFactors() method now has an overload, so that if it isn't passed an argument it performs the business end of this model.

All that happens is that the thread monitors the state of the factorial field, as if it were a socket, say. It checks to see if it is anything other than 0, and if so, it calls the CalculateFactors() method with the value of factorial as its argument and returns the ArrayList that it produces. We have also made a change in that we reset the factorial field at the start of the CalculateFactorial() method so there will always be some work to do. At the end of this method, we set factorial to equal the factorial.

Now, the frmCalculate class needs altering also. Observe the following changes:


private PeerThread threadMethods;

public frmCalculate()



private void NewFactorsThread()

//Create and invoke the delegate with the new value
UpdateValue updVal = new UpdateValue(DisplayValue);
string[] Arugs = ;
this.Invoke(updVal,Arugs);




private void cmdFactorial_Click(object sender, System.EventArgs e)

Apart from defining the new threadMethods field as a new PeerThread class, there are two important changes. We define a new method called NewFactorsThread(), which will call the CalculateFactors() method of the PeerThread class with no arguments. The rest of this method is the same.

In the cmdFactorial_Click() method, instead of just firing up the FactorialThread() method in a thread, we fire up the FactorsThread() as well, and execute them out of sequence so that calculateFactors may have to wait for calculateFactorial. You should be able to see how this can be tied into a network socket, and we will see an example of monitoring such a socket in Chapter 7.

The common problems that could occur with this kind of model are those of deadlocks and blocking. If you have a thread continually listening at a socket and getting its parameters from that socket, then that socket may need to be locked from other threads. This means, therefore, that this is a very good model to use for manager classes. We could use a thread to monitor a socket and then fire off new threads to process the contents. However, do not have more than one thread monitoring the same socket. In addition, if the thread is continually checking the socket or other resource, then it may consume more processor cycles than is necessary. As seen in the above example, you can use Thread.Sleep() to reduce the processor cycles expended.

As you will see, this example is also very similar to the Pipeline Thread Model in concept, but we are simulating a Peer Thread Model. If you click on the Calculate Factorials button, unless you have a very fast machine, you should have time to click on Calculate Factors, which will enable you to calculate and display the factors of 200, before it overwrites the textbox with the factors of , or .



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 879
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved