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 …
Second attempt … some progress. But a googly … and clean bowled ! 😩😫😣
Third … still unimpressive.
Fourth … got a result. 😏😏
Did you know !
The Way
Here is the pseudo logic of what I figured out…
Since we have 10 threads, each will pick the runnable and execute it once.
It goes ahead and uses the same key BUT different value, each time.
( if the Map is thread-safe ) We should ideally have 10 distinct values returned.
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 !
This releases all threads together.
But runnable returns a Void object … so don’t use a runnable. Use a Callable instead.
Collect all futures in a collection.
The map is thread-safe. 🔒🔒 ( This is assuming that all threads created some conflict )
🙌 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