Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     public class Program
  10.     {
  11.         static object obj = new object();
  12.         static int bilans = 1100;
  13.         static void Main(string[] args)
  14.         {
  15.             Thread t1 = new Thread(new ThreadStart(Dodaj));
  16.             Thread t2 = new Thread(new ThreadStart(Odejmij));
  17.             t1.Start();
  18.             t2.Start();
  19.  
  20.         }
  21.  
  22.         static void Dodaj()
  23.         {
  24.             lock (obj)
  25.             {
  26.                
  27.                 for (int i = 1; i <= 20; i++)
  28.                 {
  29.                     lock (obj)
  30.                     {
  31.                         bilans += 100;
  32.                         Console.WriteLine(bilans);
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.        
  38.        
  39.         static void Odejmij()
  40.         {
  41.             lock (obj)
  42.             {
  43.                
  44.                 for (int i = 1; i <= 20; i++)
  45.                 {
  46.                     lock (obj)
  47.                     {
  48.                         bilans -= 100;
  49.                         Console.WriteLine(bilans);
  50.                     }
  51.                 }
  52.             }
  53.             Console.ReadKey();
  54.         }
  55.  
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement