Thread Sick Software Engineer

Introduction

Here is a very famous and old quote related to thread sick Software Engineers: "A Software Engineer had a problem. He thinks, I know, I will use threads and solve it. Now, the Software Engineer has two problems." Thread is very good for the parallel and concurrent execution of code block, and makes the processing fast. But, it is not beneficial all the times.

Some time back, I got the chance to work with a software engineer who was crazy tfor using thread everywhere in code without thinking of its side-effects. He only observerd and saw the benefits of it. But, after some time, those threads in the code block made application worse in production environments. So, I got some experience from that thread sick engineer about how the thread is not good all the time.

Remember the below points before becoming a Thread Sick Software Engineer

Don't use threads unless there is a very convincing reason to use threads. Multiple threads bring trouble into your code. Always search better to write code so that problems can be solved.

Shared State Concurrency

Most of the programming languages use "Shared State Concurrency" threading model. More than one execution process can share the same memory resource at the same time in such kind of threading model. Here, one thread doesn't know the state of other threads and modifies the data that was not expected by another thread. So, it creates a bug.

It is Very Difficult and Painful to Debug

Code containing thread is almost impossible to debug. So, it is very difficult to find the root cause of a bug.

Remember Multiple Threads Work on the Same Memory

Multiple Threads work on the same memory and can always make friendship with Deadlock and Race-condition. And, these two terms, Deadlock and Race-condition, are very dangerous for the application.

Always Remember the Cost of Context Switching

Thread does context switching for storing and restoring the state of a process or thread so that execution can be resumed from the same point at a later time. Task scheduling, TLB flushing and sharing of CPU cache between multiple tasks happens during the Context Switching. So, the costs is performance.

Dangerous with Static Variables

Program can use static variables. And, threads share them by default so, it is not thread-safe.

Thread, Performance and Anti-Pattern

If thread is being used for fixing the performance issues then it is considered as an Anti-Pattern. Sometimes it makes performance more worst.

It is Difficult to Write Unit Tests for the Multi-Thread Code

It is really very difficult to write unit tests for the mult-thread code. It can be done with the help of Humble Pattern. Also, Thread is difficult for novice, real-world programmers. It can introduce so many obscure bugs which are very difficult to reproduce, hard to debug and fix. Normal Developers should not use it everwhere. They should be dicouraged to use thread.

Conclusion

Don't try to become Thread Sick by using thread everywhere in the code block. It will introduce so many issues which I mentioned in the above content. It is good but not in all cases. Dead-lock, Race-condition, Performance, etc. must be kept in mind before the use of threads everywhere.

Up Next
    Ebook Download
    View all
    Learn
    View all