Advertisement
wozniol

Programowanie Zaawansowane #2

Mar 31st, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.28 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. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApp13
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Calendar c1 =  Calendar.getInstance();
  15.             Calendar c2 =  Calendar.getInstance();
  16.  
  17.             Thread[] threads = new Thread[500];
  18.             for(int i = 0; i < threads.Length; i++)
  19.             {
  20.                 threads[i] = new Thread(createInstance);
  21.                 threads[i].Start();
  22.             }
  23.  
  24.             Console.WriteLine(c1 == c2);
  25.             Console.WriteLine(c1.Equals(c2));
  26.  
  27.             Console.ReadKey();
  28.         }
  29.  
  30.         private static void createInstance()
  31.         {
  32.             Calendar c = Calendar.getInstance();
  33.         }
  34.     }
  35.  
  36.  
  37.  
  38.     class Calendar
  39.     {
  40.         private static Calendar instance = null;
  41.  
  42.         private Calendar()
  43.         {
  44.  
  45.         }
  46.  
  47.         public static Calendar getInstance()
  48.         {
  49.             Thread.Sleep(new Random().Next(0, 1000));
  50.             if(null == instance)
  51.             {
  52.                 instance = new Calendar();
  53.                 Console.WriteLine("new Calendar();");
  54.             }
  55.             return instance;
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement