Threads - start using it!

Why should you use a lot of threads ?

Threads can help you take advantage of multi core CPU systems. It enables your application to run parallel and concurrent tasks. It will help your application be responsive, your application processes can share hardware resources, it will be less costly ( in terms of hardware resources) than starting a new process.[1]

BUT using threads brings up new programming challenges. An application is harder to test and debug when it is multi threaded. Even if applications are harder to test, we should embrace it because those core are going to multiply really fast and applications should take advantage of it. If you are a programmer you should really learn and teach yourself how to write applications using multiple threads because it is going to be required sooner or later.

So what do you need to know about Threads ?

In order to effectively use the underlying hardware (processor), a developer should know the underlying threading model where you are going to run your program. Even if you are programming against an abstraction of the underlying OS thread implementation, knowledge of how an OS will spin up some threads will help in understanding the behavior of an application.

Here are the threading models that operating systems use. This is how a user process thread (application that you programmed) is going to get a corresponding kernel thread in order to be processed by the CPU. So it is good to know this models because depending on the OS your applications might behave differently or you might want to have a different strategy on how you create threads.

Multi-threading Models [1] 


One-to-One Model
  Each user thread that you create will have a corresponding kernel thread
  Operating systems that uses this model: Windows XP Solaris 9 Linux
      
Many-to-Many Model
   Allows many user threads to be mapped (multiplex) to a lesser or equal number of kernel threads
  Operating systems that uses this model: Windows 2000 (with patch) HP-UX

Two-Level Model
    Basically the same as the Many-to-Many model but allows a user thread to have a corresponding kernel thread if needed 
  Operating systems that uses this model: Windows 7 (I think almost os support this model now)


Threading Abstraction via development platform

Most development platforms like C# and Java will provide you with an API that can help you manage threads in your program. This abstraction still uses the underlying threading API that the host OS provides.
Here is a link to Javas API Java Util Concurrent


So, there is a lot to know about threads but as a developer better start using it in order to take advantage of parallelism and concurrency. If you are not using threads- chances are there is a CPU out there running not getting used

[1] A. Silberschatz et al, “Threads,” in Operating System Concepts, 9th ed. Hoboken NJ,USA: Wiley 2013, ch. 4, pp. 163–204. 

Comments

Popular posts from this blog

OAuth 1.0a Request Signing and Verification - HMAC-SHA1 - HMAC-SHA256

Spark DataFrame - Array[ByteBuffer] - IllegalAurmentException

Gensim Doc2Vec on Spark - a quest to get the right Vector