コード例 #1
0
ファイル: Book.cs プロジェクト: Chang-LL/thread
 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);
 }
コード例 #2
0
ファイル: Book.cs プロジェクト: Chang-LL/thread
 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));
 }