June 9, 2010 .NET, C#, Concurrency, Performance 6 comments
June 9, 2010 .NET, C#, Concurrency, Performance 6 comments
I agree that title doesn’t promise a lot of interesting stuff at first glance especially for experienced .net developers. But unless you encounter some issue due to incorrect usage of timers you will never think that root is in timers.
System.Threading.Timer vs. System.Windows.Forms.Timer
In few words what are differences between Threading and Forms timers just to start with something.
System.Threading.Timer executes some method on periodic bases. But what is interesting is that execution of method is performed in separate thread taken from ThreadPool. In other words it calls QueueUserWorkItem somewhere internally for your method at specified intervals.
System.Windows.Forms.Timer ensure as that execution of our method will be in the same thread where we’ve created timer.
What if operation takes longer than period?
Let’s now think what will happen if the operation we set for execution takes longer than interval.
When I have following code:
my application behaves well – prints “a” twice a second. I took a look for number of threads in Task Manager and it stays constantly (7 threads).
Let now change following line: Thread.Sleep(500) to Thread.Sleep(8000). What will happen now? Just think before continue to read.
I’m almost completely sure that you predicted printing “a” every second after 8 seconds have passed. As you already guessed each of the “a” printings are scheduled in separate threads allocated from ThreadPool. So… amount of threads is constantly increasing… (Every 1.125 seconds :) )
Issue I’ve been investigating
Some mister X also figured out that Console.WriteLine(“a”) is critical and should run in one thread, at least because he is not sure how much does it take to execute Thread.Sleep(500). To ensure it will run in one thread he decided to have lock, like in code below:
Yes, this code ensures that section under lock is executed in one thread. And you know this code works well unless your execution takes few hours and you will be out of threads and out of memory. :) So that is an issue I’ve been investigating.
My first idea was System.Windows.Forms.Timer
My first idea was to change this timer to the System.Windows.Forms.Timer, and it worked well in application, but that application is able to run in GUI and WinService modes. But there are so many complains over interned to do not use Forms.Timer for non UI stuff. Also if you put Forms.Timer into your console application it will simply not work.
Why System.Timers.Timer is good toy?
System.Timers.Timer is just wrapper over System.Threading.Timer, but what is very interesting is that it provides us with more developer-friendly abilities like enabling and disabling it.
My final decision which fixes issue is to disable timer when we are diving into our operation and enable on exit. In my app timer executes every 30 seconds so this could not be a problem. Fix looks like:
And it looks that we don’t need lock there, but I left it there just to be sure is case if SomeOperation will be called from dozen of other threads.
MAKE DECISION ON TIMER BASING ON THIS TABLE (from msdn article)
System.Windows.Forms | System.Timers | System.Threading | |
---|---|---|---|
Timer event runs on what thread? | UI thread | UI or worker thread | Worker thread |
Instances are thread safe? | No | Yes | No |
Familiar/intuitive object model? | Yes | Yes | No |
Requires Windows Forms? | Yes | No | No |
Metronome-quality beat? | No | Yes* | Yes* |
Timer event supports state object? | No | No | Yes |
Initial timer event can be scheduled? | No | No | Yes |
Class supports inheritance? | Yes | Yes | No |
* Depending on the availability of system resources (for example, worker threads |
I hope my story is useful and when you will be searching like “C# Timer Threads issues” or “Allocation of threads when using timer” you will find my article and it will help you.
May 19, 2010 .NET, Concurrency, Fun No comments
Today I was playing with exception handling and threading in .NET. It was really fun.
Do you know guys that like to have everything in global try-catch block? I have two news for them.
Bad one
Exception will not be caught in try-catch block if it was thrown in another thread. Those guys also could think that Application.ThreadException could help them to catch those.
static
void
Application_ThreadException(object
sender, System.Threading.ThreadExceptionEventArgs
e)
{
MessageBox.Show(“This is
something that I would not recommend you to have in your application.”);
}
But indeed this event fires only if exception has been thrown as result of Windows message or any other code that runs in same thread were your WinForms application lives. I tried to use two timers to verify that.
System.Windows.Forms.Timer – which ensures that code you have in your tick method runs in the same thread as your application. In this case I got message box.
System.Threading.Timer – which runs in separate thread, so my app just crashed.
But if those guys are writing all code in Form1.cs file… then maybe it worth for them to have handling of Application.ThreadException event :)
“Good” one
There is event which will be fired when exception is thrown from any code/thread withing your application domain. It is AppDomain.UnhandledException and it occurs when exception is not caught.
Lets take a look on code snippet that shows this:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
static
void
currentDomain_UnhandledException(object
sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show(“This is
shown when ANY thread is thrown in ANY point of your Domain.”);
}
To test this I created Threading.Timer and fired it every 2 seconds and throwing exception on each tick, I put breakpoint into event. I got everything expected and application after that failed.
But one of our smart guys could guess to put Thread.Sleep(1000000); into handler code like below:
Everyone is happy. Exception is thrown each 2 seconds or less and UI continue to respond. I hope you already see what is wrong with all of this.
This screenshot talks for itself:
And as you know process could have up to 1024 threads. My process is not exclusion and it also crashed after reached that number.
Ok, looks like guys should change their mind.
Hope it was a bit fun.
May 16, 2010 .NET, Concurrency, MasterDiploma, Performance 4 comments
In recent time I was interacted with multithreading in .NET.
One of the intersting aspects of it is performance. Most of books says that we should not overplay with performance, because we could introduce ugly-super-not-catching bug. But since I’m using multithreading for my educational purposes I allow myself play with this.
Here is some list of performance tips that I’ve used:
1. UnsafeQueueUserWorkItem is faster than QueueUserWorkItem
Difference is in verification of Security Privileges. Unsafe version doesn’t care about privileges of calling code and runs everything in its own privileges scope.
2. Ensure that you don’t have redundant logic for scheduling your threads.
In my algorithm I have dozen of iterations on each of them I perform calculations on long list. So in order to make this paralleled I was dividing this list like [a|b|c|…]. My problem was in recalculating bounds on each iteration, but since list is always of the same size I could have calculating bounds once. So just ensure that don’t have such crap in your code.
3. Do not pass huge objects into your workers.
If you are using delegate ParameterizedThreadStart and pass lot of information with your param object it could decrease your performance. Slightly, but could. To avoid this you could put such information into some fields of the object that contains method for threading.
4. Ensure that you main thread is also busy guy!
I had this piece of code:
Do you see where I have performance gap? Answer is in utilizing my main thread. Currently it is only scheduling some number of threads (GridGroups) to do some work and than it waits for them to accomplish. If we divide work to approximately equivalent partitions, we could gave some work to our main thread, and in this way waiting time will be eliminated.
Following code gives us persormance increase:
5. ThreadPool and .NET Framework 4.0
Guys, from Microsoft improved performance of the ThreadPool significantly! I just changed target framework of my project to the .Net 4.0 and for worst cases in my app got 1.5x time improvement.
What’s next?
Looking forward that I also could create more sophisticated synchronization with Monitor.Pulse() and Monitor.WaitOne().
Good Threading Reference
Btw: I read this quick book Threading in C#. It is very good reference if you would like to remind threading in C# and to find some good tips on sync approaches.
P.S. If someone is interested if I woke up at 8am. (See my previous post). I need to say that I failed that attempt. I woke at 12pm.
May 14, 2010 Book Reviews, Success No comments
Today I finished listening to another book on success. It is called “The Law Of Success” by Napoleon Hill. Book is represented in 16 lessons.
I think that it worth to read or listen to this book. It shows up lot of different tips that you can use to achieve your strongly defined and be biggest goal of your life. I know that most of books on success talk about main goal of your life, but this one I think is the strongest on this requirement.
So it says that you need to define for yourself goal of your life, and… what is more interested… you should keep repeating its formulation every day few times. Hill just refers to autosuggestion as one of the key concepts on getting your results.
Honestly yesterday I tried to repeat for myself that I will wake up 2 hours early than I usually do, and it did not help… sad. Today I will have another try. So “I will wake up at 8! I will wake up at 8! I will wake up at 8! I will wake up at 8! I will wake up at 8! I will wake up at 8! I will wake up at 8! I will wake up at 8! I will wake up at 8! I will wake up at 8 regardless of Saturday! I will wake up at 8am! I will wake up at 8! I will wake up at 8 and will be able to do what I have planned! I will wake up at 8! Guys, I will wake up at 8! I will wake at 8am! I will really wake up at 8am! I’m going to wake up at 8 o’clock tomorrow! I will wake up at 8 am! I will do that! I will wake up at 8! I will wake up at 8!”
Ok, I did what he asked, now I will see if it will really help me, and will share with you :)
May 9, 2010 C#, Java, MasterDiploma, Opinion 4 comments
Is CLR worse than JVM or it is just because my code is bad?
Today I had conversation with one man, who is great defender and evangelist of Java related technologies.
We talked about some algorithm implemented by me with C#. That is implementation of SOM algorithm and Concurrency stuff for it. My concurrency implementation doesn’t show time improvements unless grid size is big. He was able to achive better result with small grid sizes.
There could mean two reasons:
Taking into consideration that I’m getting better results when grid is of bigger sizes I could suppose that in my algorithm there is code which takes constant (or near that) time and is not paralleled.
When I look at picture of Task Manager when my programm is executing I see that second processor has gaps when it does nothing:
This could mean only one: I need to take better look what else is executed in one thread in my application and could be paralleled.
Converting C# code to Java
But back to that man and Java. I said to myself that it is possible to move my code to Java if something.
C# to Java Converter
Download C# to Java Converter demo version from here. Since it is demo you could not convert more than 1000 lines of your code. Order costs 119$.
Because of that I was forced to remove all not necessary code. I removed all concrete classes that I’m not using and GUI, but that did not help. I did not know how much should I delete more.
Number of lines in my project/solution
I googled for line numbers in C# and found this nice tool:
Now I know that my program has about 4000 lines of code. I left about 980 lines of code in two projects that I needed and was porting them to Java separately.
Converter GUI
And converted them to Java:
Is conversion an easy task?
Conversion could be painful if you have a lot of code that interacts with system, like threads, reading and writing to files, reading configuration file, etc.
Also it was not able to recognize ‘var’ keyword. It shows me this:
I moved back to my C# code and changed all var to the explicit types. And regenerated Java code.
There were some troubles when I’m delivering from List<T> and doing some stuff around that.
This code:
I rewrote manually to:
Also Converter was not able to convert this:
The biggest challenge is ThreadPool and synchronizing threads tech-nicks. This requires lot of google search that shows me that instead of ManualResetEvent I could use CyclicBarrier and how to utilize thread queuing in Java.
P/S I need to find gaps in my code to show that at least CLR and myself are not so much bad things in this world :)
May 7, 2010 MasterDiploma, Ukrainian No comments
Одним із найважливіших завдань для реалізації масштабування обчислень є знаходження частин алгоритму, які можна та які є оптимально розбити на деякий набір незалежних обчислень.
Велика кількість роботи по розробці масштабування обчислинь алгоритму SOM була зроблена за останні роки. Взагалі кажучи існує два основних підходи, якими є розбиття гратки (map-partitioning, MP) та розбиття даних (data-partitioning, DP).
В MP, SOM є розподілена поміж декількох потоків, таким чином, що кожен потік обробляє кожен вхідний вектор і модифікує свою частину гратки. Такий підхід зберігає порядок оновлень записаних у формулі [осн. формула оновлення]. Найбільшим недоліком цього методу є велика ціна синхронізації потоків, потоки повинні бути синхронізовані опісля кожного разу, коли був знайдений нейрон-переможець, а також кожного разу, коли пройшло оновлення. Таким чином MP є ефективним для хардверної реалізації або ж для систем із спеціальною архітектурою для паралелізації.
В DP, вхідні дані розподілені поміж потоками, так, що кожен потік містить частину вхідних даних та проганяє її на своїй копії гратки. Як бачимо цей підхід не вимагає частої синхронізації, таким чином це є перевагою над MP. Проте такий підхід не відповідає формулі оновлення, що змушує використовувати його із варіаціями SOM, зоокрема batch SOM. Та навіть попри це batch SOM не є популярним методом, із-за двох основних причин:
1. Він вимагає щоб усі вхідні вектори були наперед відомими, що не є завжди можливо.
2. Насправді дуже важко отримати добре структуровану карту на відміну від класичного алгоритму.
Існує також багато інших розробок. Для прикладу в одній із таких автори пропонують підхід схеми Master-Slave, яка базується на тому факті, що із часом радіус, у якому ми модифікуємо нейрони зменшується, а тому два послідовні нейрони-переможці можуть не мати спільних областей. Таким чином можна обробляти ці нейрони разом із частиною матриці в паралельних потоках. Master потік слідує за основними кроками класичного алгоритму, а Slave пробує здійснити обчислення в паралельному потоці для наступного нейрона переможця якщо області не перетинаються.
Мною була здійснена реалізація MP підходу до розпаралелення алгоритму із невеликими відмінностями.
Основною відмінністю є те, що в стандартній реалізації MP гратка розбивається на декілька підграток, скажімо на n частин. Опісля для заданого вхідного вектора ми шукаємо нейрон переможець у кожній із підграток в паралельних потоках. Як тільки він був знайдений, ми запускаємо модифікацію на тих самих частинах гратки. Якщо глянути на рисунку нижче, то можна побачити, що в такій ситуації для модифікації ефективно використовується лишень четвертий потік, перший та третій швидко впорюються із роботою, а другий потік взагалі простоює.
Я пропоную знайдену множину сусідів знову поділити на рівні частини, як це зображено на наступному рисунку, проте із певною відмінністю. Якщо множина нейронів для модифікації є більшою за число M, то я розбиваю цю множину на ту ж саму кількість що й розбивав гратку, і запускаю оновлення в різних потоках (перевикористовуються вже існуючі). Якщо число нейронів для модифікаціх менше за M, то модифікація проганяється в одному потоці. Число М має залежати від ціни синхронізації і ще потребує додатковго дослідження.
Такий підхід дає помітні покращення коли гратка є дуже виликих розмірів.
May 1, 2010 Book Reviews, Success 2 comments
I just listened to one very interesting audio book. Book title is “How to Win Friends and Influence People”. And I think that it worth to read, or listen to it as I did, because it teaches you how to interact with others. If you are going to grow in your career you indeed need such stuff to know.
This book is of psychological type and author (Dale Carnegie) is talking about interaction with other people. Mainly it talks about how to correctly behave when you want someone to be on your side.
Book could be very useful for managers and for people in their lives. For developer it could be useful in future when you are going to drive few people in your team. If you just starting as a senior developer you already should have some experience driving junior stuff. When you are Technical Leader you need to drive whole team, you need to establish correct processes in your team and one of the hardest tasks in all of this is to make others work on tasks you assign them in the way that they will want to work on that.
Sometimes there are situations when you see somebody’s mistakes and there should be a way to show them this, but at the same time not make them your enemies – they should look at you as at friend, who wants to help.
Book has a lot of different examples from real life when you convince others to do what you want, but for them it looks like they really want to do it, because it seems useful for them.
There are a lot of rules in book. You could go and see contents of the book.
Go to this wiki page to see main points of book. (I recommend).
But now I will try to list down what I remember:
I know that you heard about that a hundred times. But when you will spent 10,5 hours on listening to this book and when author talks about this during all the time. This will ensure that you remember it and probably will force you to try it in real life. You could start with your team members or with you wife. I think that I will try with my girl just today… or even before I will meet her, since I’m now disconnected from internet and typing this in bus road to Lviv. So will be able post this in evening.
I would recommend this book if you have time for such things or if you could get audio version as I did.
P.S. I tried today some tips from book – it works! You should try.
April 30, 2010 Certification, Success No comments
Why would I need to pass that exam at all?
There are many arguments to pass the exam. Most of them are simple bureaucracy. First of all, your resume will have additional strength if you will add few certification records on the bottom. Secondly, many companies are interested to have their employees to be certified, or maybe they want to make you promotion road more hard :) so to allow you to be promoted you need to pass some set of certificates and cope with lot of other requirements. At least this is situation which I have in my company.
Why would I need this exam for my blog?
Very easy: when YOU are on my blog first time, you read something and create your thought on topic you just read and then you decide to subscribe or not to subscribe. Question is following: what is guarantor that it worth to subscribe to my blog? And answer is “Nothing”. But what will be your reaction if you will see MVP logo at the top-right of my blog? Are my chances better? Or course they are.
Why else would I need this exam?
Following point is very important but honestly I could not put it above (I will describe why). The point is to enhance you knowledge and learn something new. Of course during preparation to exam you read some books and search over MSDN on topics you are interested and which will be measured. But in real life you could be experienced .NET developer and still have difficulties to pass exam. In other words I have doubts if 10 years experienced developer could pass exam without at least some special preparation to it.
What else did I miss?
Of course – self-confidence. In order to be successful you need to feel yourself confident in things you do. In order to grow you should have something behind you, that could you help stand when you are with people who have certificates. And plus to that it is always very enjoyable to know that you know more then others. But a secret: do not show them that. It is very important. Do not praise yourself, let others do it instead.
How to prepare and how did I prepare?
You could find a lot of very interesting posts over internet on how to prepare.
I like this one:
http://lukecummins.me.uk/2009/08/70-536-my-tips-for-this-microsoft-exam/
I even found other blogs that are about certification.
My recommendation number 1: google on how to prepare… and you will find a lot of interesting stuff.
Also while you are trying some demo tests on MeasureUp or tests from TrainingKit and if you are not confident about answer do not try to remember it – go and figure answer in depth!
Also I wrote some tips on how to read TrainingKit. At least this works for me:
How was it?
Night before exam I felt confident about exam, but was still worrying.. so I was preparing till 3am. In the morning I came to work as usual and did few things… reviewed ones code, checked e-mails… expressed my thoughts on demo we should have today and wrote e-mail to team members that I will be out for about 2 hours. My company is Microsoft’s parner so I do not need to move to other location – just get down 5 floors.
First couple of questions I was nervous, but then I felt that I’m confident with most of questions. I marked only 2 out of 40 for additional review. When I finished I got message saying that I PASSED EXAM with score 907. This means that I answered incorrectly for about 4 questions. I think that this is very good result.
Picture which I get is like following:
My next target is probably 70-505. Passing it is point of honour, because I already failed it twice because of overflow of confidence.
Also as I’m active in preparing developers meeting for developers from my project and I’m thinking to teach our developers in order to get certification – covering some areas from exam.
Express your thoughts on passing this exam.
April 28, 2010 English No comments
Today’s lesson was not usual. My usual teacher is in vacation, so we have conference calls and we are trying to imitate different situations during conf. calls.
Situation
Initially there was an agreement with the Client for a particular scope of project. After some time the Client adds more features that require additional time for investigation and execution – in particular he wants to change the user interface (UI) from HTML to FLEX. It’s high time to start working on user interface but the Client is silent regarding reconsideration of the agreement terms. Project Manager has to take charge of conducting a negotiation with the Client and decides to invite Tech Lead, QA and Senior Developer for that.
Roles has been assigned to students. And initially I got TL role, but when we dialed up we discovered that 3 of us are not present, so teacher decided that I could take 3 roles in one time.
Roles are following:
Tech Lead: You do not know FLEX and you do not have time to study it because it is time consuming. But you believe that it is possible to fulfill all the requirements if your PM can find a specialist in that technology and make him TL of that part of the project.
Senior Developer: You have used FLEX before. You are not an expert in it but you can do the task. However, you require more time (you have your own tasks which you have to finish in time) and you also need more people. Anyway, there should be reconsideration of the agreement terms.
QA: You know that whenever a new feature is added, it always involves adding test cases and plans rewriting. You are 100% sure that this change will require extra effort for testing. Besides, the integration of a new feature will lead to the additional integration testing.
I was trying to be in all of them and I would say that I succeeded in those.One of interesting things about this is that I was dialing from my desk, and one of the colleagues was really confused about what I’m talking out there… Maybe he was concerned if I’m not mad, but teacher said that it was tremendous :)
I got a lot of fun and I really appreciate that my company game me possibility to learn English in lot of fun and enjoyable ways.
April 27, 2010 Certification, Success 8 comments
It turns out that I’m eligible to pass exam 70-536 for free – my company will pay for me, but before I’m required to pass Brainbench C# exam with higher than 4.0 score.
And I just did it – my score is 4.03.
You could see my results using following link: http://www.brainbench.com/transcript.jsp?pid=9108054
So now I could use this logo on my blog (see somewhere on the right):
Also in e-mail which I got is following information:
You are eligible for a certificate! Would you like to receive it? | ||||||
|
How do you think should I pay for that? Please let me know.
Also following information says that I have weak areas in implementing Services and Data Consumers. Need to check-out in more details about that.
|
||||||||||||||
|
Guys, it is quite easy to pass this. And don’t loose chance, this exam is free till 30 April.