DEV Community

Achilonu Chinwendu Faustina
Achilonu Chinwendu Faustina

Posted on

DAY 4 CODE CHALLENGE

PRINT IN ORDER

Todays challenge was a very daunting one, I migrated from JavaScript to Python3 because of the unavailability of 'print in order' LeetCode problem in JavaScript version. Having started my coding career with python before I drifted to JavaScript I was able to scale through this challenge.

QUESTION
Suppose we have a class:
public class Foo{
public void first() {print("first");}
public void second() {print("first");}
public void third() {print("first");}
}

Here, Foo will be passed to three different threads:
Thread A = first()
Thread B = second()
Thread C = third()

TASK
Design a mechanism and modify the program to ensure that the second() is executed after the first() and the third is executed after the second().

PROCEDURE
Lock method was applied to solve this problem. I started with two locked locks, whereby first thread unlocks the first lock that the second thread was waiting on while the second thread unlocks the second lock that the third thread was waiting on

OUTCOME

Image description

Top comments (0)