Using System;
Using System. Threading;
Using System. Collections;
// Your own thread wrapper
Interface IBookCollection
{
Void Clear ();
Void Add (Book n );
Book GetBook (string ISBN );
Bool IsSync {get ;}
Object Syncroot {get ;}
}
Class Book
{
Public string Name;
Public string ISBN;
Public string Author;
Public string Publisher;
}
Class BookLib: IBookCollection
{
Internal Hashtable bk = new Hashtable (10 );
Public virtual void Clear ()
{
This. bk. Clear ();
}
Public virtual void Add (Book B)
{
Console. Write ("Adding Book for ThreadID:" +
Thread. CurrentThread. GetHashCode ());
Thread. Sleep (1000 );
This. bk. Add (B. ISBN, B );
}
Public virtual Book GetBook (string ISBN)
{
Console. Write ("Getting Book for ThreadID:" +
Thread. CurrentThread. GetHashCode ());
Return (Book) bk [ISBN];
}
Public virtual bool IsSync
{
Get
{
Return false;
}
}
Public virtual object Syncroot
{
Get
{
Return this;
}
}
Public BookLib Sync ()
{
Return Sync (this );
}
Public static BookLib Sync (BookLib bc)
{
If (bc = null)
{
Throw new ArgumentNullException ("bc ");
}
If (bc. GetType () = typeof (SyncBookLib ))
{
Throw new InvalidOperationException
("BookLib reference is already Sync .");
}
Return new SyncBookLib (bc );
}
Public static IBookCollection Sync (IBookCollection acc)
{
If (acc = null)
{
Throw new ArgumentNullException ("acc ");
}
If (acc. GetType () = typeof (SyncBookLib ))
{
Throw new InvalidOperationException
(
"BookLib refernce is already sync ."
);
}
Return new SyncBookLib (acc );
}
}
Sealed class SyncBookLib: BookLib
{
Private object SyncRoot;
Private object bookLib;
Internal SyncBookLib (IBookCollection acc)
{
BookLib = acc;
SyncRoot = acc. Syncroot;
}
Public override void Clear ()
{
Lock (SyncRoot)
{
Base. Clear ();
}
}
Public override void Add (Book B)
{
Lock (SyncRoot)
{
Base. Add (B );
}
}
Public override Book GetBook (string ISBN)
{
Lock (SyncRoot)
{
Return (Book) bk [ISBN];
}
}
Public override bool IsSync
{
Get
{
Return false;
}
}
Public override object Syncroot
{
Get
{
Return SyncRoot;
}
}
}
Class Test
{
Private static BookLib acc;
Private static int n = 0;
[STAThread]
Static void Main (string [] args)
{
IBookCollection accInstanse = new BookLib ();
String temp = null;
Console. WriteLine ("Enter whether you want to synchronize or not synchronize Y/N ");
Temp = Console. ReadLine ();
If (temp. ToLower ()! = "N ")
{
Acc = new SyncBookLib (accInstanse );
}
Else
{
Acc = new BookLib ();
}
Thread [] threads = {
New Thread (Run), new Thread (Run), new Thread (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 ("Totao Number of Books Added" + n );
Console. Read ();
}
Static void Run ()
{
For (int I = 0; I <2; I ++ ){
Book bk = new Book ();
Bk. Author = "Author: Xiao Li ";
Bk. Name = "C #" + I;
Bk. Publisher = "People's Publishing House ";
Bk. ISBN = (n ++). ToString ();
Acc. Add (bk );
}
}
}