0
Posted on 6/11/2009 by Shyam and filed under
So here is a dilemna of the day! What would be your decision process when designing an application between using a single thread of execution, multiple threads, multiple processes or multiple computers?

I don't know what is your decision process, but while designing the application and determining the approach, there are several things that I consider such as -
  • How resource intensive the operation is?
  • Does doing it in background thread make sense? (For example, when user asks for archive data of last 5 years in pdf format!).
  • What are the technical limitations? Like, controls created in UI thread can not be accessed in another thread.
  • What happens if the processing fails?
These are some of the points that I consider. If we want to isolate it from running code so that running code doesn't fail with it, we can invoke it in a separate process. Multiple computers should be considered when expected volume is too high. To better summarise -

Single Thread - If the application is very linear in its operation, I prefer to design it single threaded. One of the main advantages it has is ease of implementation and maintenance since there is no need to maintain concurrency. One of the main disadvantages for a single threaded application is inability to perform more than one task of a program module.

Multiple Threads - If the application is not very linear in operation and has multiple tasks that can be executed simultaneously, independent of each other, then it should be designed as multi threaded. When using multi threaded approach for the application, a single program module can be broken logically and different parts of program can be run simultaneously. This is achieved by using separate threads for each task. One such example is an application that needs to monitor incoming emails and also process outgoing mails. Both of the tasks are part of the same module and shares resources but can be executed independent of each other. One of the main advantages of multithreading approach is concurrent processing. Also, compared to multiple processing it is more light-weight form of concurrency. Since all threads in multi threaded module shares same address space, data on the heap can be readily accessed by all threads. There are couple of disadvantages for multithreaded approach. Designing and maintaining multi threaded application is more complex compared to single threaded application as special care needs to be taken to maintain concurrency. Also, in a multi threaded application, error in one thread can potentially bring down all other running threads for the process.

Multiple Processes – When the same application needs to run multiple instances at the same time, it needs to be designed to be able to run multiple processes on the same machine. I would use multiple processes of the application if there is a need to run multiple instances of the application for different users with different permissions or for a subset of data independent from another instance of the application. This would be useful when state of one process instance needs to be kept independent from another process instance of the application. Since processes are insulated from each other by OS, an error in one process cannot bring down another process.

Multiple Computers – One of the primary reasons to implement the application on multiple computers is when the application utilizes client specific resources. In a client server application user can not access application entirely from central location and needs to access the application from client systems. In such cases the application needs to be installed on each system from where user intends to use the application. This approach enables application to utilize resources from client system and also utilize its processing power to run.
0
Responses to ... Single Thread, Multiple Threads, Multiple Processes or Multiple Computers???

Post a Comment