Leet Code - Concurrency Flashcards

1
Q

https://leetcode.com/problems/print-in-order/description/

  1. Print in Order
  2. 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 know 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.

Example 1:

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.
Example 2:

Input: nums = [1,3,2]
Output: “firstsecondthird”
Explanation: The input [1,3,2] means thread A calls first(), thread B calls third(), and thread C calls second(). “firstsecondthird” is the correct output.

A

class Foo {

private CountDownLatch latchAfterFirst;
private CountDownLatch latchAfterSecond;
public Foo() {
    latchAfterFirst = new CountDownLatch(1);
    latchAfterSecond = new CountDownLatch(1);
}

public void first(Runnable printFirst) throws InterruptedException {
    
    // printFirst.run() outputs "first". Do not change or remove this line.
    
    printFirst.run();
    latchAfterFirst.countDown();
}

public void second(Runnable printSecond) throws InterruptedException {
    
    // printSecond.run() outputs "second". Do not change or remove this line.
    latchAfterFirst.await();
    printSecond.run();
    latchAfterSecond.countDown();
}

public void third(Runnable printThird) throws InterruptedException {
    
    // printThird.run() outputs "third". Do not change or remove this line.
    latchAfterSecond.await();
    printThird.run();
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

https://leetcode.com/problems/print-foobar-alternately/description/

Print FooBar Alternately
Suppose you are given the following code:

class FooBar {
public void foo() {
for (int i = 0; i < n; i++) {
print(“foo”);
}
}

public void bar() {
for (int i = 0; i < n; i++) {
print(“bar”);
}
}
}
The same instance of FooBar will be passed to two different threads:

thread A will call foo(), while
thread B will call bar().
Modify the given program to output “foobar” n times.

Example 1:

Input: n = 1
Output: “foobar”
Explanation: There are two threads being fired asynchronously. One of them calls foo(), while the other calls bar().
“foobar” is being output 1 time.
Example 2:

Input: n = 2
Output: “foobarfoobar”
Explanation: “foobar” is being output 2 times.

A

class FooBar {
private int n;
private boolean turn;

public FooBar(int n) {
    this.n = n;
    turn = true;
}

public void foo(Runnable printFoo) throws InterruptedException {
    synchronized(this) {
        for (int i = 0; i < n; i++) {
            while(!turn) {
                wait();
            }
            // printFoo.run() outputs "foo". Do not change or remove this line.
            printFoo.run();
            turn = false;
            notifyAll();
        }
    }
}

public void bar(Runnable printBar) throws InterruptedException {
    synchronized(this) {
        for (int i = 0; i < n; i++) {
            while(turn) {
                wait();
            }
            // printBar.run() outputs "bar". Do not change or remove this line.
            printBar.run();
            turn = true;
            notifyAll();
        }
    }
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly