Click here to Skip to main content
15,885,914 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.2K   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.
<?xml version="1.0"?>
<doc>
    <assembly>
        <name>Synchronization</name>
    </assembly>
    <members>
        <member name="T:com.sarmal.articles.en.synchronization.BankAccount">
            <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>Simple interface for a bank account.</description>
            </summary>
        </member>
        <member name="M:com.sarmal.articles.en.synchronization.BankAccount.Empty">
            <summary>
            Clears the account.
            </summary>
        </member>
        <member name="M:com.sarmal.articles.en.synchronization.BankAccount.Add(System.Double)">
            <summary>
            Adds money to account.
            </summary>
            <param name="money">amount to add.</param>
        </member>
        <member name="P:com.sarmal.articles.en.synchronization.BankAccount.Balance">
            <summary>
            The current balance.
            </summary>
        </member>
        <member name="P:com.sarmal.articles.en.synchronization.BankAccount.IsSynchronized">
            <summary>
            Is the class synchronized.
            </summary>
        </member>
        <member name="P:com.sarmal.articles.en.synchronization.BankAccount.SyncRoot">
            <summary>
            Synchronization root.
            </summary>
        </member>
        <member name="T:com.sarmal.articles.en.synchronization.BankAccountImpl">
            <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>Implementation class of the bank account.</description>
            </summary>
        </member>
        <member name="F:com.sarmal.articles.en.synchronization.BankAccountImpl.sum">
            <summary>
            Total amount of money in the bank.
            </summary>
        </member>
        <member name="M:com.sarmal.articles.en.synchronization.BankAccountImpl.Empty">
            <summary>
            Clears the bank account.
            </summary>
        </member>
        <member name="M:com.sarmal.articles.en.synchronization.BankAccountImpl.Add(System.Double)">
            <summary>
            Adds money to the account.
            </summary>
            <param name="money">The amount of money to add.</param>
        </member>
        <member name="M:com.sarmal.articles.en.synchronization.BankAccountImpl.Synchronized(com.sarmal.articles.en.synchronization.BankAccountImpl)">
            <summary>
            Returns a synchronized wrapper to class.
            </summary>
            <param name="impl">The class to wrap.</param>
            <returns>A syncronized wrapper.</returns>
        </member>
        <member name="M:com.sarmal.articles.en.synchronization.BankAccountImpl.Synchronized(com.sarmal.articles.en.synchronization.BankAccount)">
            <summary>
            Returns a syncronized wrapper to the class.
            </summary>
            <param name="acc">The interface.</param>
            <returns>A synchronized wrapper.</returns>
        </member>
        <member name="M:com.sarmal.articles.en.synchronization.BankAccountImpl.Wrap(com.sarmal.articles.en.synchronization.BankAccount)">
            <summary>
            Wraps the object and returns a thread-safe wrapper.
            If <code>acc</code> is synchronized, returns it. Else
            returns a wrapped version of it.
            </summary>
            <param name="acc">The object </param>
            <returns></returns>
        </member>
        <member name="P:com.sarmal.articles.en.synchronization.BankAccountImpl.Balance">
            <summary>
            Overall balance so far.
            </summary>
        </member>
        <member name="P:com.sarmal.articles.en.synchronization.BankAccountImpl.IsSynchronized">
            <summary>
            Is the class safe for multi-threaded access.
            </summary>
        </member>
        <member name="P:com.sarmal.articles.en.synchronization.BankAccountImpl.SyncRoot">
            <summary>
            Syncronization root for the class.
            </summary>
        </member>
        <member name="T:com.sarmal.articles.en.synchronization.BankAccountImpl.SyncAccount">
            <summary>
            The private inner class used to wrap the class.
            </summary>
        </member>
        <member name="F:com.sarmal.articles.en.synchronization.BankAccountImpl.SyncAccount.syncRoot">
            <summary>
            Synchronization root
            </summary>
        </member>
        <member name="F:com.sarmal.articles.en.synchronization.BankAccountImpl.SyncAccount.bankAccount">
            <summary>
            Reference to the wrapped BankAccount instance.
            </summary>
        </member>
        <member name="M:com.sarmal.articles.en.synchronization.BankAccountImpl.SyncAccount.#ctor(com.sarmal.articles.en.synchronization.BankAccount)">
            <summary>
            COnstructor.
            </summary>
            <param name="acc">The BankAccount class to wrap.</param>
        </member>
        <member name="M:com.sarmal.articles.en.synchronization.BankAccountImpl.SyncAccount.Empty">
            <summary>
            Clears the bank account.
            </summary>
        </member>
        <member name="M:com.sarmal.articles.en.synchronization.BankAccountImpl.SyncAccount.Add(System.Double)">
            <summary>
            Adds money to the bank account.
            </summary>
            <param name="money">The amount of money to add.</param>
        </member>
        <member name="P:com.sarmal.articles.en.synchronization.BankAccountImpl.SyncAccount.Balance">
            <summary>
            Returns the current balance.
            </summary>
        </member>
        <member name="P:com.sarmal.articles.en.synchronization.BankAccountImpl.SyncAccount.IsSynchronized">
            <summary>
            returns true since the class is a synchronized wrapper.
            </summary>
        </member>
        <member name="P:com.sarmal.articles.en.synchronization.BankAccountImpl.SyncAccount.SyncRoot">
            <summary>
            Returns the syncroot of its wrapped member.
            </summary>
        </member>
        <member name="T:com.sarmal.articles.en.synchronization.Test">
            <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>
        </member>
        <member name="F:com.sarmal.articles.en.synchronization.Test.acc">
            <summary>
            Member to test
            </summary>
        </member>
        <member name="M:com.sarmal.articles.en.synchronization.Test.Main(System.String[])">
            <summary>
            Main entry point.
            </summary>
            <param name="args">varargin</param>
        </member>
        <member name="M:com.sarmal.articles.en.synchronization.Test.run">
            <summary>
            runnable method for the thread army.
            </summary>
        </member>
    </members>
</doc>

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