static void Main0() { acc = new BookLib(); if (Console.ReadLine().Length > 0) { acc = acc.Synchronized(); //or BookLib.Synchroized(acc) } Thread[] threads = { new Thread(new ThreadStart(Run)), new Thread(new ThreadStart(Run)), new Thread(new ThreadStart(Run)), }; foreach (var t in threads) { t.Start(); } foreach (var t in threads) { t.Join(); } for (int i = 0; i < n; i++) { Book bk = acc.GetBook(i.ToString()); if (bk != null) { Console.WriteLine("Book: " + bk.Name); Console.WriteLine("ISBN: " + bk.ISBN); Console.WriteLine("Publisher: " + bk.Publisher); Console.WriteLine("Auther: " + bk.Author); } } Console.WriteLine("Total Number of books added " + n); }
public static BookLib Synchronized(BookLib bookLib) { if (bookLib == null) { throw new ArgumentException("bookLib"); } if (bookLib.GetType() == typeof(SyncBookLib)) { throw new InvalidOperationException( "BookLib reference is already synchronized"); } return(new SyncBookLib(bookLib)); }