Day 4 of 10: Ingressive For Good DataStructures and Algorithms Challenge

Threading in Python

Day 4 of 10: Ingressive For Good DataStructures and Algorithms Challenge

Phewww!!! It's day 4 and the presher is getting werser.

Today's task was entirely new ground for me. It was my first time hearing about threading and MulltiThreading.

If it's your first time too, don't worry, I gat you!

What is Threading?

Threading in Python or other programming languages allows a user to run different parts of the program in a concurrent manner and make the design of the program simpler.

During threading, different processors run on a single program and each one of them performs an independent task simultaneously.

If you're like me, you're probably munching on a snack, listening to a song and reading this post at the same time.

This is possible because you have different body parts (threads) to perform the functions: chewing, listening and reading.

That's multithreading in real life.

To understand threading and MulltiThreading, check out these replies on quora.

Onto the task for today!

Instructions

Suppose we have a class:

public class Foo {

                 public void first() { print("first");

                 public void second() { print("second"); }

                 public void third() { print("third"); }
}

The same instance of Foo" will be passed to three different threads. Thread A will call first(), thread B will call second(), and thread C will call third(). Design a mechanism and modify the program to ensure that second() is executed after first(), and third() ) is executed after second()*.

Note:

We do not kow how the threads will be scheduled in the operating system, even though the numbers in the input seem to imply the ordering. The input format you see is mainly to ensure our tests' comprehensiveness.

TestCase:

Input: nums = [1,2,3]

Output: "firstsecondthird"

Explanation: There are three threads being fired asynchronously. The input [1,2,3] means thread A calls first(), thread B calls second(), and thread C calls third(). "firstsecondthird" is the correct output.

My Solution

I scoured the internet to understand the concept of threading. And after a million and one searches, I finally found a code snippet I could understand.

The code made use of the threading module. Each function was assigned a thread. Each thread was started using the start() method and the join() method with the time.sleep() method to wait till the previous thread is executed.

Using this didn't solve all testcases. So I took a closer look at the code. Now I had a better understanding of the task, I figured increasing the seconds passed to the time.sleep() for each function would increase the time it'd take the function to wait thus ensuring that the first function executes first then the second, then the third.

And it worked! But the memory usage(14.6mb) was much higher than that of other submissions so I thought if people could figure out a more efficient way of solving the problem. I could too.

Based on a hunch, I removed the entire block of code that assigned threads to each function and removed the threading module. I left the time.sleep() method for each function and voila! The code still worked! And this time with less memory usage(14.4mb).

I was overjoyed but I had one last trick up my sleeve. I removed the time.sleep() for the first function and ensured the time in seconds for the second and third arguments were 0.05s apart. And you know it... It worked and this time had a memory usage of 14.3mb.

And thats how I solved an entirely new topic in a few hours.

You can view the submission details below.

day 4 submission.jpg

What a day!