1114. Print in Order

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.

 

Constraints:

  • nums is a permutation of [1, 2, 3].

Rust Solution

use std::sync::Condvar;
use std::sync::Mutex;

struct Foo {
    seq: (Mutex<i32>, Condvar),
}

impl Foo {
    fn new() -> Self {
        let seq = (Mutex::new(1), Condvar::new());
        Foo { seq }
    }

    fn first(&self, print_first: impl FnOnce()) {
        let (lock, cvar) = &self.seq;
        let mut g = cvar
            .wait_while(lock.lock().unwrap(), |seq| *seq != 1)
            .unwrap();
        *g = 2;
        print_first();
        cvar.notify_all();
    }

    fn second(&self, print_second: impl FnOnce()) {
        let (lock, cvar) = &self.seq;
        let mut g = cvar
            .wait_while(lock.lock().unwrap(), |seq| *seq != 2)
            .unwrap();
        *g = 3;
        print_second();
        cvar.notify_all();
    }

    fn third(&self, print_third: impl FnOnce()) {
        let (lock, cvar) = &self.seq;
        let mut g = cvar
            .wait_while(lock.lock().unwrap(), |seq| *seq != 3)
            .unwrap();
        *g = 4;
        print_third();
        cvar.notify_all();
    }
}

#[test]
fn test() {
    use std::sync::mpsc::channel;
    use std::sync::Arc;
    use threadpool::ThreadPool;

    let nums = vec![1, 2, 3];
    let fooo = Arc::new(Foo::new());
    let (tx, rx) = channel();

    let pool = ThreadPool::new(4);

    for x in nums {
        let tx = tx.clone();
        let fooo = fooo.clone();
        pool.execute(move || match x {
            1 => fooo.first(|| tx.send("first".to_string()).unwrap()),
            2 => fooo.second(|| tx.send("second".to_string()).unwrap()),
            3 => fooo.third(|| tx.send("third".to_string()).unwrap()),
            _ => {}
        })
    }
    pool.join();

    let ss: Vec<String> = rx.try_iter().collect();
    let res = ss.join("");
    let ans = "firstsecondthird".to_string();
    assert_eq!(ans, res);

    let nums = vec![1, 3, 2];
    let fooo = Arc::new(Foo::new());
    let (tx, rx) = channel();

    let pool = ThreadPool::new(4);

    for x in nums {
        let tx = tx.clone();
        let fooo = fooo.clone();
        pool.execute(move || match x {
            1 => fooo.first(|| tx.send("first".to_string()).unwrap()),
            2 => fooo.second(|| tx.send("second".to_string()).unwrap()),
            3 => fooo.third(|| tx.send("third".to_string()).unwrap()),
            _ => {}
        })
    }
    pool.join();

    let ss: Vec<String> = rx.try_iter().collect();
    let res = ss.join("");
    let ans = "firstsecondthird".to_string();
    assert_eq!(ans, res);
}

Having problems with this solution? Click here to submit an issue on github.