[Java] 94% runtime efficiency, using synchronized, wait and notify, explained
Although I consider myself proficient in Java, this was a great refresher on synchronized, wait, and notify.  I did some research.  Based on Sonar, any wait() c
Although I consider myself proficient in Java, this was a great refresher on synchronized, wait, and notify.  I did some research.  Based on Sonar, any wait() call should check a condition in a while loop, so I have two booleans to indicate if the first and second have been printed.  I declared two objects, firstObject and secondObject to use for synchronization.  Method first synchronizes on firstObject and can then run printFirst(), and does a notify on firstObject to signal completion of the first method.  Method second is synchronized on firstObject, has a while loop on !first, and waits for the notify on firstObject.  When the while loop is exited, it synchronizes on secondObject, prints second, sets second to true, and does a notify on secondObject to signal completion of the second method.  Method third does the same things, synchronizing on secondObject, looping while !second, and waits for the notify on secondObject.  When the loop is exited, printThird can be run. \n\nIf you like this post, please upvote!\n```\nclass Foo {\n\n    public Foo() {\n        first = false;\n        second = false;\n    }\n\n    Object firstObject = new Object();\n    Object secondObject = new Object();\n    \n    boolean first = false;\n    boolean second = false;\n    \n    public void first(Runnable printFirst) throws InterruptedException {\n        synchronized(firstObject) {\n\t\t    // printFirst.run() outputs "first". Do not change or remove this line.\n            printFirst.run();\n            first = true;\n            firstObject.notify();\n        }\n    }\n\n    public void second(Runnable printSecond) throws InterruptedException {\n        synchronized(firstObject) {\n            while (!first) {\n                try {\n                    // Calling wait() will block this thread until another thread calls notify() on the object.\n                    firstObject.wait();\n                } catch (InterruptedException e) {\n                    // Happens if someone interrupts your thread.\n                }\n            }\n            synchronized(secondObject) {\n                // printSecond.run() outputs "second". Do not change or remove this line.\n                printSecond.run();\n                second = true;\n                secondObject.notify();\n            }\n        }\n    }\n\n    public void third(Runnable printThird) throws InterruptedException {\n        synchronized(secondObject) {\n            while (!second) {\n                try {\n                    // Calling wait() will block this thread until another thread calls notify() on the object.\n                    secondObject.wait();  \n                } catch (InterruptedException e) {\n                    // Happens if someone interrupts your thread.\n                }\n            }\n        }\n        // printThird.run() outputs "third". Do not change or remove this line\n        printThird.run();\n    }\n}
['Java']