Lecture thumbnail 0:00 / 0:00 So far, the kind of single stones that we’ve looked were using lazy.
So lazy of t and lazy of T gave us two things.
It gave us laziness, obviously, but it also gave us thread safety during initialization.
But sometimes what you can do is you can avoid this problem altogether by making sure that instead of
having one singleton per the entire app domain as you would with a lazy object, you can instead have
one singleton per thread.
And this is also a possibility that is afforded by the net framework.
I’m sure you’ve heard about the thread static attribute, but there is also a class called Thread Local,
which is kind of like lazy, except that it is per thread as opposed to a kind of a global thing.
So let me show you how this can work so we can have some sort of class A per thread.
Singleton So a per thread.
Singleton Once again, I can make it sealed just to, just for additional safety, so to speak.
This is something which would have a private constructor as before as we did with the other singletons.
But instead of using lazy, what you would do here is you would have a thread local variable, so you
would have a private static thread local per thread singleton.
Let’s go with thread instance as opposed to instance because it better communicates what’s actually
going on.
So this would be a thread local and then the constructor is very similar to the lazy constructor in
the sense that here you provide a lambda which initializes the object that you actually want to work
with, and then subsequently, if you want to return that instance, once again you would have a public
static per thread singleton instance.
Once again, we could call it a thread instance, but here I would just do thread instance dot value
like so.
So this is how you would expose it.
And then let’s actually verify that this thing works.
So in the constructor, what I’m going to do is I’m going to record the thread ID, so here I would
have a public int ID and I would say here that ID is equal to thread dot current thread dot manage thread
ID.
This way we’re going to actually see that the the IDs of the objects as they are constructed.
So let’s try to set up a scenario where we have two threads and we’re going to take a look at whether
or not those singletons do in fact operate within these separate threads.
So I’ll have var T1 equals, let’s use TPL task factory, start new.
So I’ll start a new task where we are going to just do a console.writeline.
So here I’m going to say that, well, we’re in thread one and I’m going to get the thread ID of a per
thread singleton.
So in actual fact, let’s do it using plus.
So here I would say per thread singleton.instance.id that should give us one ID and then we can replicate
this.
So I can copy this and I can paste it down here.
So this is going to be task two.
And let’s, let’s specify that we’re in task two and let’s do this twice just so that we get to see
that when we call per thread singleton instance a second time, we don’t get another instance on that
thread.
So now that I have the two threads running, I’m just going to do task.
Wait, oh wait on both T1 and T2 and we can run this and we can see what we actually get here.
All right.
So as you can see, the values on task two are four and four.
So we’re getting a unique object here in both of these cases.
And in t one, we have the value of three.
So this is an alternative which completely avoids this situation of thread safety by creating one instance
of your class per thread.
Now it’s really up to you to find the scenarios where this is relevant.
But if you can afford or if you absolutely need one instance per thread as opposed to one instance per
the entire application, then this is one of the possible ways to go.
Although once again, this kind of lifetime is also available when you configure an IOC container.
So it might make sense to use a dependency injection container with a lifetime of per thread instead
of doing it manually.
But if you want to do it manually, then this is the way you would do it.
Play Play Play Play Play Stop Play Start Play