Reputation: 514
I am wondering if invoking a constructor blocks other threads in the program? I was looking into concurrency report and found a long thread blocking other threads, and it's stack always has a constructor in it.
EDIT:
After the investigation, I found out, that the lock occurred because of the DB calls from another request (I ran an ASP.NET app).
To the question, as I found out in this article, memory allocation does not block threads, except when GC is invoked, as Servy pointed out.
Upvotes: 1
Views: 324
Reputation: 62248
Everything depends on concrete context. Constructor is just another method, with special duty to construct (allocate memory) for your instance. You can construct object inside another thread, or block thread inside constructor, but it has nothing to do with constructor itself. Blocking is about a flow of your program.
Upvotes: 0
Reputation: 203802
No, it does not. It's possible for this specific constructor to be doing something that is causing other threads to block, but the act of calling a constructor doesn't, in and of itself, block all other threads.
One thing you may be noticing though is that the garbage collector does need to block the execution of all threads when it's running. If you have a thread that is allocating lots and lot of objects, to the point that it's causing GC collections more than is desirable, you could be blocking your threads for noticeable periods of time.
Upvotes: 3