![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
|
The old saying goes something like, "Hey stupid, don't use static methods when you're going to call that function a lot from multiple threads, if you want to have any kind of good performance" at least I think that's how it goes. |
|
Anyway, I get the idea behind it, because it's locking the single instance of code in the entire app domain for now and all the life of the app, and everyone accessing it has to wait in line. |
#2
| |||
| |||
|
|
dave.dolan <davedolan (AT) discussions (DOT) microsoft.com> wrote: The old saying goes something like, "Hey stupid, don't use static methods when you're going to call that function a lot from multiple threads, if you want to have any kind of good performance" at least I think that's how it goes. I don't know who says that. Anyway, I get the idea behind it, because it's locking the single instance of code in the entire app domain for now and all the life of the app, and everyone accessing it has to wait in line. Only if there's synchronization involved. If there isn't (and usually there needn't be) all the threads can run through the same method at the same time. It's easy to verify this yourself: using System; using System.Threading; public class Test { static void Main() { for (int i=0; i < 5; i++) { new Thread(new ThreadStart(Count)).Start(); } } static void Count() { for (int i=0; i < 10; i++) { Console.WriteLine (i); Thread.Sleep(200); } } } If static methods had to queue all the requests up, you'd see 0 1 2 3 4 5 6 7 8 9 five times. Instead, you're more likely to see 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 (etc) -- Jon Skeet - <skeet (AT) pobox (DOT) com http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too |
#3
| |||
| |||
|
|
Ok, thank you very much, I feel better after having read that! I'm glad I asked instead of proceeding with my misinformation. |

![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |