Skip to content

techwiddeep.com

Menu
  • About me
  • Contact Us
Menu

Failure : My Teacher ( Threads )

Posted on November 13, 2021December 29, 2021 by Deep.Kulshreshtha

How my failure, combined with my efforts to overcome helped me !

Prologue

One of the most loved and hated subjects in software engineering – Threads. While they solve big problems of concurrency and performance … they create even trickier problems of Deadlocks and race conditions.

.

Threads and me – we were never good friends. I did not understand them and they refused to help me understand. Don’t get me wrong … I tend to know all the objects they process, and they seem to know exactly where I’ll create a situation.

But, our relationship grew over time. Serendipity … I call thy name !

 

The Obstacle

Writing code in a thread-safe manner is tricky enough. Imagine writing tests to prove that a piece of code is thread-safe. That was precisely what I had to do !

.

Lord knows we programmers hate writing tests.

That being the case, tests for thread safety is unheard of. We virtually never write test cases for thread safety.

But Murphy’s law exists for a reason. To throw the nut in the most inaccessible corner. And have evaluators ask us to write tests for thread safety.

.

The Struggle

I was supposed to ravish the evaluation, on the first night. But, it was “confusion” at first sight.

It looked at me confused. Wondering whether I was shy, unwilling, or simply (programmatically) impotent. The fact was I simply did not know how to “take care” of it 😌😌. That too in a language I wasn’t very well versed with – Kotlin.

Some help maybe? Get my juices flowing.

I rushed to Google, asking for help. And boy, Google never disappoints !

.

But the subject of Threads is so tricky that copying never helps. So, I made myself comfortable and started meditating on all the Gyan I could gather.

Having absorbed a bit … I took the first shot. Failure!

All sorts of questions overwhelmed me …

●How would I trigger multiple threads all at once ?
●Would the behavior of each thread differ ?
●What function would each thread perform ?
●What behavior from each thread could be used to verify thread concurrency ?

Second attempt … some progress. But a googly … and clean bowled ! 😩😫😣

Third … still unimpressive.

Fourth … got a result. 😏😏

.

Did you know !

●A Map’s add/ put method returns the previous value, for a key, value pair. Therefore, the first time a pair is added, the returned value is null. On the second try, the first value is returned.
●A map’s remove method returns the previous value.

.

The Way

Here is the pseudo logic of what I figured out…

1.Create an executor service with 10 threads. 🏭
2.Create a runnable, and trigger it 10 times. ⛹⛹⛹⛹⛹⛹⛹⛹⛹⛹

Since we have 10 threads, each will pick the runnable and execute it once.

3.The runnable adds data to a Map. 🗺

It goes ahead and uses the same key BUT different value, each time.

4.Since add() method returns the previous value.

( if the Map is thread-safe ) We should ideally have 10 distinct values returned.

5.If we trigger threads in a loop, they will process in a loop.

To avoid this … use a Thread control mechanism like a CountDownLatch. Make all threads wait on the latch.

Figured out a practical use of CountDownLatch(). Hallelujah !

6.Trigger the Latch. 🚦🚦

This releases all threads together.

7.We need to collect the results of all threads. 🛀 🛀

But runnable returns a Void object … so don’t use a runnable. Use a Callable instead.

8.The trigger of each thread will give a Future<> object.

Collect all futures in a collection.

9.Loop over the collection to get each response and collect them into a Set.
10.If the Set size is 10 – this means threads did NOT interfere with one another.

The map is thread-safe. 🔒🔒 ( This is assuming that all threads created some conflict )

11.Used IntelliJ to convert Java to Kotlin.

.

🙌 Mission accomplished 🙌

.

The Result

Nada, zilch, nothing !

The company responded with a lame-ass review comment 😂😁😅😆. Regardless, I was happy about having learned something new. More so about concepts, I was so close to and did not know well ( despite believing otherwise ).

While I am sure, the happiness will be short-lived. That is, I will fail at something else very soon ! But here I am enjoying the fleeting moment of happiness. Enjoying my journey from failure to a step of success !

A small failure + some effort = becoming better

Better self -> Lead to -> Better results

.

© 2025 techwiddeep.com | Powered by Superbs Personal Blog theme