Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Threading;
- namespace BarrierDemo
- {
- class Account
- {
- public double Balance { get; set; }
- public Account(double sum)
- {
- Balance = sum;
- }
- public int CalcCreaditPoint()
- {
- Thread.Sleep(400);
- return (int)Balance / 10;
- }
- public double CalcUBalance(double currenty)
- {
- Thread.Sleep(400);
- return currenty * Balance;
- }
- }
- class Program
- {
- public static List<Account> accounts = new List<Account>();
- public static Barrier myBarrier = new Barrier(3);
- public static double balanceTotal = 0;
- public static double creaditPointTotal = 0;
- static void Main(string[] args)
- {
- CreateAccounts();
- new Thread(CalculateAccountsTotal).Start();
- new Thread(CalculateTotalCreditPoints).Start();
- myBarrier.SignalAndWait();
- Console.WriteLine("Total balance: " + balanceTotal);
- Console.WriteLine("Total credit points: " + creaditPointTotal);
- Console.ReadLine();
- }
- private static void CalculateTotalCreditPoints(object obj)
- {
- double total = 0;
- foreach (Account account in accounts)
- {
- Thread.Sleep(500);
- total += account.CalcCreaditPoint();
- }
- creaditPointTotal = total;
- myBarrier.SignalAndWait();
- }
- private static void CalculateAccountsTotal()
- {
- double total = 0;
- foreach (Account account in accounts)
- {
- Thread.Sleep(500);
- total += account.Balance;
- }
- balanceTotal = total;
- myBarrier.SignalAndWait();
- }
- private static void CreateAccounts()
- {
- double[] sums = { 12000, 8000, 10000, -400, 5400, 8000, 2240 };
- foreach (double sum in sums)
- {
- accounts.Add(new Account(sum));
- Thread.Sleep(200);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement