Java Solution - (Approach 1: Using Object lock, Approach 2: Using Countdown Latch)
Intuition\nWe need to suspend the thread printing second until first completes, and third until second completes.\n\n# Approach 1 - Using Object lock\nUse synch
# Intuition\nWe need to suspend the thread printing second until first completes, and third until second completes.\n\n# Approach 1 - Using Object lock\nUse `synchronsed` block to guard the block of code, and suspend the thread using `buzy waiting` method until `first`, `second` is not printed in order.\n\n\n# Code\n```java\nclass Foo {\n    private int currentNum;\n\n    public Foo() {\n        this.currentNum = 1;\n    }\n\n    public void first(Runnable printFirst) throws InterruptedException {\n        synchronized (this) {\n            while (currentNum != 1) {\n                wait();\n            }\n            // printFirst.run() outputs "first". Do not change or remove this line.\n            printFirst.run();\n            currentNum++;\n            notifyAll();\n        }\n    }\n\n    public void second(Runnable printSecond) throws InterruptedException {\n        synchronized (this) {\n            while (currentNum != 2) {\n                wait();\n            }\n            // printSecond.run() outputs "second". Do not change or remove this line.\n            printSecond.run();\n            currentNum++;\n            notifyAll();\n        }\n    }\n\n    public void third(Runnable printThird) throws InterruptedException {\n         synchronized (this) {\n            while (currentNum != 3) {\n                wait();\n            }\n            // printThird.run() outputs "third". Do not change or remove this line.\n            printThird.run();\n            currentNum++;\n            notifyAll();\n        }\n    }\n}\n```\n\n\n# Approach 2 - Using CountDownLatch\nHere we make use of two countdown latches, \nthe second thread waits on first latch to countDown to 0, and third thread waits on second latch to countDown to 0.\n<br>\nThe code is self explanatory.\n\n# Code\n```java\nclass Foo {\n    private CountDownLatch firstLatch;\n    private CountDownLatch secondLatch;\n\n    public Foo() {\n        this.firstLatch = new CountDownLatch(1);\n        this.secondLatch = new CountDownLatch(1);\n    }\n\n    public void first(Runnable printFirst) {\n        printFirst.run();\n        firstLatch.countDown();\n    }\n\n    public void second(Runnable printSecond) throws InterruptedException {\n        firstLatch.await();\n        printSecond.run();\n        secondLatch.countDown();\n    }\n\n    public void third(Runnable printThird) throws InterruptedException {\n        secondLatch.await();\n        printThird.run();\n    }\n}\n```
['Java']