Java compareAndSet()
java\nimport java.util.concurrent.atomic.AtomicInteger;\nclass Foo {\n\n    private AtomicInteger integer;\n    \n    public Foo() {\n        integer = new Atom
```java\nimport java.util.concurrent.atomic.AtomicInteger;\nclass Foo {\n\n    private AtomicInteger integer;\n    \n    public Foo() {\n        integer = new AtomicInteger();    \n    }\n\n    public void first(Runnable printFirst) throws InterruptedException {\n        while(!integer.compareAndSet(0, 1));\n        printFirst.run();\n    }\n\n    public void second(Runnable printSecond) throws InterruptedException {\n        while(!integer.compareAndSet(1, 2));\n        printSecond.run();\n    }\n\n    public void third(Runnable printThird) throws InterruptedException {\n        while(!integer.compareAndSet(2, 3));\n        printThird.run();\n    }\n}\n```
[]