[ Pobierz całość w formacie PDF ]
.The condition for theBarrier class is satisfied when all of the Tasks participating in the algorithm have called theSignalAndWait() method.If a Task calls the method before the required number of calls has been made,it is made to wait.The number of calls is specified in the class constructor and can be altered using theAddParticipant() and RemoveParticipant() methods.The Tasks performing the algorithm call the SignalAndWait() method when they reach the end of aphase.Not only does the Barrier release any waiting Tasks when the current phase ends, but it resetsautomatically, meaning that subsequent calls to SignalAndWait() will make Tasks wait until the counterreaches 0 again and another phase is complete.When creating a new instance of Barrier, you can specify a System.Action that will be performed atthe end of each phase and before the Tasks are notified that they should start the next one.you can seean example of this in the listing below.Table 4-6 summarizes key members of the Barrier class.Table 4-6.Selected Members of the System.Threading.Barrier ClassMember DescriptionAddParticipant() Increment the number of calls that must be made toAddParticipants(int) SignalAndWait() before a phase completes.RemoveParticpant() Decrement the number of calls that must be made toRemoveParticpants(int) SignalAndWait() before a phase completes.SignalAndWait() Signal the primitive that the current Task hascompleted the current phase, and wait indefinitelyfor the other Tasks to do the same.SignalAndWait(CancellationToken) Function like SignalAndWait(), but give up waitingSignalAndWait(Int32)for the other Tasks if they have not all signaled theSignalAndWait(TimeSpan)primitive before the specified time has passed or theSignalAndWait(Int32, CancellationToken)specified cancellation token is cancelled.SignalAndWait(TimeSpan, CancellationToken)CurrentPhaseNumber Report the current phase number, incremented eachtime the SignalAndWait() method has been called thenumber of times specified in the constructor.131 CHAPTER 4  % COORDINATING TASKSContinuedMember DescriptionParticipantCount Return the number of calls to the SignalAndWait()method that will mark the end of a phase and releaseany waiting Tasks.ParticipantsRemaining Return the number of participants that have yet tosignal the end of the current phase.Listing 4-12 demonstrates how to create and use the Barrier class.When the Barrier instance iscreated, two constructor arguments are supplied: the number of Tasks that must call SignalAndWait()before the primitive condition is met and a System.Action(Barrier) that will be called each time thecondition is met (the listing uses a lambda expression to define System.Action).In the example, we create an array of BankAccounts and a set of Tasks that perform a simplemultiphase algorithm against using the accounts.In the first phase, the Tasks enter a loop to addrandom amounts to the account they are working with and then signal the Barrier to indicate they havereached the end of the current phase.The Barrier then executes the constructor Action, which sums the individual balances into thetotalBalance variable.The second phase of the algorithm begins, where each Task reduces the balanceof its account by 10 percent of the difference between the current balance and the total balance, aprocedure that would not have been possible prior to all Tasks completing the first phase.At the end ofthe phase, the Tasks signal the Barrier again, which marks the end of the second phase and triggers theconstructor action again.Listing 4-12.Using the Barrier Classusing System;using System.Threading;using System.Threading.Tasks;namespace Listing_12 {class BankAccount {public int Balance {get;set;}}class Listing_12 {static void Main(string[] args) {132 CHAPTER 4  % COORDINATING TASKS// create the array of bank accountsBankAccount[] accounts = new BankAccount[5];for (int i = 0; i {// zero the balancetotalBalance = 0;// sum the account totalsforeach (BankAccount account in accounts) {totalBalance += account.Balance;}// write out the balanceConsole [ Pobierz caÅ‚ość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • chiara76.htw.pl