Copied. C # self-written wrapper with multiple threads

Source: Internet
Author: User

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 );
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.