Click here to Skip to main content
15,891,423 members
Articles / Mobile Apps

Synchronization Basics and Concept of Using a Synchronized Wrapper

Rate me:
Please Sign up or sign in to vote.
2.69/5 (6 votes)
21 Feb 20056 min read 53.3K   352   19  
To prevent concurrent access to a data structure, one way is to use thread-aware objects, the other is to make a thread-safe wrapper for the object.
namespace com.sarmal.articles.en.synchronization
{
	using System;
	using System.Threading;

	/// <summary> 
	///		<disclaimer>
	///			<d>2005� volkan.ozcelik                                                   </d>
	///			<d>_______________________________________________________________________</d>
	///			<d>                                                                       </d>
	///			<d>You may use and modify this code as you like, either for professional  </d>
	///			<d>development or as per your personal interest.                          </d>
	///			<d>Just do not remove this disclaimer and the following meta information. </d>
	///			<d>_______________________________________________________________________</d>
	///			<d>                                                                       </d>
	///			<d>Note that I always like to get mentioned where my code and/or know-how </d> 
	///			<d>is being used. However, I do not require it. I am more willing to help </d>
	///			<d>people who inform and support me with their content or comments.       </d>
	///			<d>_______________________________________________________________________</d>
	///		</disclaimer>
	///		<meta-data>
	///			<file-created>2005.02.21</file-created>
	///			<version>1.0.0.@20050221</version>
	///			<article>"Synchronization Basics and Concept of Using a Synchronized Wrapper"</article>
	///			<author>
	///				<name>volkan.ozcelik</name>
	///				<e-mail>volkan.ozcelik@gmail.com</e-mail>
	///			</author>
	///		</meta-data>
	///		<description>The test case.</description>
	///	</summary>
	public class Test
	{
		/// <summary>
		/// Member to test
		/// </summary>
		private static BankAccountImpl acc;

		/// <summary>
		/// Main entry point.
		/// </summary>
		/// <param name="args">varargin</param>
		public static void Main(string[] args) {
			
			acc = new BankAccountImpl();
			/* 
			 * remove the commented line below and 
			 * rebuild project to see the difference.
			 */
			//acc = BankAccountImpl.Synchronized(acc);

			Thread[] army = {
				new Thread(new ThreadStart(run)),
				new Thread(new ThreadStart(run)),
				new Thread(new ThreadStart(run)),
				new Thread(new ThreadStart(run)),
				new Thread(new ThreadStart(run)),
				new Thread(new ThreadStart(run)),
				new Thread(new ThreadStart(run)),
				new Thread(new ThreadStart(run)),
				new Thread(new ThreadStart(run)),
				new Thread(new ThreadStart(run)),
				new Thread(new ThreadStart(run)),
				new Thread(new ThreadStart(run))
			};

			Console.WriteLine("Total balance is expected to be:  {0}.", army.Length * 10);

			for(int i=0;i<army.Length;i++) {
				army[i].Name = "Thread-" + i;
				Console.WriteLine("Starting " + army[i].Name);
				army[i].Start();
			}

			for(int i=0;i<army.Length;i++) {
				Console.WriteLine("Joining " + army[i].Name);
				army[i].Join();
			}

			Console.WriteLine("The current balance is {0}.", acc.Balance);

			Console.ReadLine();
		
		}

		/// <summary>
		/// runnable method for the thread army.
		/// </summary>
		private static void run() {
			Console.WriteLine(Thread.CurrentThread.Name + " entered add.");
			for(int i=0;i<10;i++) {
				Console.WriteLine(Thread.CurrentThread.Name + ": Balance before add : {0}", acc.Balance);
				acc.Add(1);
				Console.WriteLine(Thread.CurrentThread.Name + ": Balance after add : {0}", acc.Balance);
			}
			Console.WriteLine(Thread.CurrentThread.Name + " exited add.");
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Turkey Turkey
Volkan is a java enterprise architect who left his full-time senior developer position to venture his ideas and dreams. He codes C# as a hobby, trying to combine the .Net concept with his Java and J2EE know-how. He also works as a freelance web application developer/designer.

Volkan is especially interested in database oriented content management systems, web design and development, web standards, usability and accessibility.

He was born on May '79. He has graduated from one of the most reputable universities of his country (i.e. Bogazici University) in 2003 as a Communication Engineer. He also has earned his Master of Business Administration degree from a second university in 2006.

Comments and Discussions