Programming - Threading
Thursday, January 6, 2011 7:25:56 PM
So what is this threading all about? Threading is deciding when said subroutine/function is going to be executed and if it's going to be paused/resumed and when. In another words it's timing of the subroutines. Threading can be complicated if you make it so, but it can also be pretty easy if you keep track of it.
In similar way works your processor, the processes that you think are being executed in reality are threaded to keep multitasking flawless. You can see an example on the left.
Back to programming though, programmers use threading to solve problem such as this one:
We have two classes, one is a main class, lets call that one A, and the other is a object class, let's call it B. When class A is executed it creates an instance of an object - class B. Class A then calls a subroutine located in class B, the subroutine is then executed in the background, the class A immediately continues its further execution. In another words while the subroutine from class B was called class A is still executing. That saying if we were to use the information from the subroutine from class B while it's execution is not finished yet, we would get an error, just like "Heap size" error. That's when threading becomes useful. We could pause execution of main subroutine in class A until subroutine from class B finishes executing.
I know this was all theoretical, but as I said every programming language does it differently. These are some good tutorials written by professionals on threading:
IBM: Java Threads Tutorial
Microsoft: C# Threading TutorialRedHat: A Tutorial on Threads in Perl