public static BookLib Synchronized(BookLib bc) { if (bc == null) { throw new ArgumentNullException("bc"); } if (bc.GetType() == typeof(SyncBookLib)) { throw new InvalidOperationException("BookLib reference is already synchronized"); } return(new SyncBookLib(bc)); }
static void Main(string[] args) { acc = new BookLib(); if (args.Length > 0) { acc = acc.Synchronized(); //or BookLib.Synchronized(acc); } Thread[] threads = { new Thread(new ThreadStart(Run)), new Thread(new ThreadStart(Run)), new Thread(new ThreadStart(Run)) }; foreach (Thread t in threads) { t.Start(); } foreach (Thread 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("Author:" + bk.Author); } } Console.WriteLine("TotalNumber of books added " + n); Console.ReadLine(); }