-----------------------------------------------------------
New Message on BDOTNET
-----------------------------------------------------------
From: Rlabh
Message 11 in Discussion
Hi Harsha,
Threads will be executed on Round Robin basis that is on time slice and
based on the priority. Now, in the case of singleton object, the first thread
(not necessarily the first thread started) that will create the singleton object
will return that object and all other threads will return the same singleton
object. What I mean to say is all the threads will be accessing the singleton
class based on the basis of time slicing and once the instance of the ohject is
created, all other threads will get the same object. Check the code below,
class SingletonClass
{
// Fields
private static
SingletonClass SingletonInstance;
// Constructors (protected)
protected
SingletonClass()
{
// do something
}
// Methods
public static SingletonClass
GetSingletonInstance()
{
// Support multithreaded
applications through
// "Double checked locking" pattern which
avoids
// locking every time the method is
invoked
if( SingletonInstance == null
)
{
// Only one thread can obtain a
mutex
Mutex mutex = new Mutex();
//1
mutex.WaitOne();
if( SingletonInstance == null
)
//2
Singletoninstance = new
SingletonClass(); //3
mutex.Close();
}
return
SingletonInstance;
}
}
Once the SingletonInstance is not null, then the same object created before
will be returned back.
Let's take two threads, T1 and T2
Thread T1 enters the GetSingletonInstance() method above.
Thread T1 enters the synchronized block at //1 because instance is
null.
Thread T1 is preempted by thread T2.
Thread T2 enters the GetSingletonInstance() method.
Thread T2 attempts to acquire the lock at //1 because instance is still
null. However, because thread T1 holds the lock, thread T2 is blocked at
//1.
Thread T2 is preempted by thread T1.
Thread T1 executes and because instance is still null at //2, creates a
Singleton object and assigns its reference to instance.This is the reason why it
is required to check for Null second time.
Thread T1 exits the synchronized block and returns instance from
GetSingletonInstance() method.
Thread T1 is preempted by thread T2.
Thread T2 acquires the lock at //1 and checks to see if instance is
null.
Because instance is non-null, a second Singleton object is not created and
the one created by thread T1 is returned.
If there are other threads, then all the other threads will get the same
object as long as the single instance is not null. The thread time slicing will
be based on the priority though the priority can be overruled by the OS. So, to
say thread will maintain FIFO is not correct fully and it will depend on the
time slots that a particular thread will have.
I didn't get a proper link to corroborate myself, however, you can go
through the URL
http://www-106.ibm.com/developerworks/java/library/j-dcl.html?dwzone=java
that explains the same thins using Java.
Regards,
Rajesh K Labh
----- Original Message -----
From:
HarshaPriya
To: [EMAIL PROTECTED]
Sent: Thursday, August 21, 2003 7:08
AM
Subject: Re: .net remoting (singleton
object implementation)
New Message on BDOTNET
.net
remoting (singleton object implementation)
Reply
Reply to Sender Recommend
Message 10 in
Discussion
From: HarshaPriya
Hi ,
Thanks for your replies. But
it would be great if you could get me a solid
proof of
working or article which says so it happens or are you 100% sure
of
it? This is because I have to implement this in a real
time critical project
where I do not want to take chances.
In case if its not too sure that the
thread queuing is done,
I am all the more ready to implement a FIFO with a
semaphore
and stuff. Thanks a lot for your time and help.
Thanks
and Regards,
Harsha
Priya
_________________________________________________________________
Australia
ahoy! Fly there for free.
http://www.australia.singaporeair.com/en/contest_main.asp On
Singapore
Airlines!
View other groups in this
category.
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
If you do not want to receive future e-mail from this MSN group, or if you received
this message by mistake, please click the "Remove" link below. On the pre-addressed
e-mail message that opens, simply click "Send". Your e-mail address will be deleted
from this group's mailing list.
mailto:[EMAIL PROTECTED]