Saturday, November 21, 2015

LinkedIn: H2O



http://cse.unl.edu/~ylu/csce351/notes/Two-Alternative-Solutions-for-Building-H2O.pdf
http://www.cs.berkeley.edu/~kubitron/cs162/hand-outs/synch-solutions.html
Building H2O
  1. Correctness constraints
    1. Each hydrogen thread waits to be grouped with one other hydrogen and oxygen before returning
    2. Each oxygen thread waits for two other hydrogens before returning
    3. Only one thread access shared state at a time
  2. There is only one condition any thread will wait for, i.e. a water molecule being formed. However, it will be necessary to signal hydrogen and oxygen threads independently, so  we will use two condition variables, waitingH and waitingO.
  3. It will be necessary to know the number of hydrogen and oxygen threads in the monitor.   But it would be more useful to know how many hydrogen and oxygen threads have been assigned and have not been assigned to water molecules; let these be int wH (number of waiting hydrogens), wO (number of waiting oxygens),aH (number of assigned hydrogens), and aO (number of assigned oxygens). These are all initialized to 0.
http://www.fgdsb.com/2015/01/03/h2o-problem/
https://paulamarcu.wordpress.com/2014/08/03/program-h2o/

http://www.mitbbs.com/article_t/JobHunting/32348515.html
http://www.mitbbs.com/article_t/JobHunting/32331973.html
http://shanhe.me/2015/03/13/implement-an-h-method-and-an-o-method-to-produce-a-water-molecular
Both methods are to be called by multiple threads. Each thread calling either method becomes blocking and when one thread finds that there are already at least two threads calling H() and at least one thread calling O(), let two threads calling H() and one thread calling O() resume from blocking in order to produce a water molecular.
LinkedIn: H2O
实现两个函数: H() and O(), 这两个函数会被多线程调用。当一个线程调用H或O时
,如果当前已经有至少两个线程call H和一个线程call O。那么让两个call H和一个
call O的线程返回(产生一个水分子),其他的都block。
对Lock不是很熟,请教一下,一个thread call H2O.h(),LOCK就被take了,那其他的thread就没办法call h()了吧?那H就一直为1?

public class H2O {
    static final Lock LOCK = new ReentrantLock();
    static final Condition ENOUGH_H = LOCK.newCondition();
    static final Condition ENOUGH_O = LOCK.newCondition();
    static int H = 0;
    static int O = 0;
    static void check() {
        if (H >= 2 && O >= 1) {
            ENOUGH_H.signal();
            ENOUGH_H.signal();
            ENOUGH_O.signal();
            H -= 2;
            O -= 1;
        }
    }
    public static void h() {
        LOCK.lock();
        try {
            check();
            ++H;
            ENOUGH_H.awaitUninterruptibly();
        } finally {
            LOCK.unlock();
        }
    }
    public static void o() {
        LOCK.lock();
        try {
            check();
            ++O;
            ENOUGH_O.awaitUninterruptibly();
        } finally {
            LOCK.unlock();
        }
    }
}`

  1. public class H2O {
  2.  
  3.   static final int H = 0;
  4.   static final int O = 1;
  5.   static final Lock LOCK = new ReentrantLock();
  6.   static final Condition ENOUGH[] = {LOCK.newCondition(), LOCK.newCondition()};
  7.   static final int NEEDED[] = {2, 1};
  8.   static final int COUNT[] = {0, 0};
  9.  
  10.   static void run(int index) {
  11.     LOCK.lock();
  12.     try {
  13.       if(COUNT[H] >= NEEDED[H] && COUNT[O] >= NEEDED[O]) {
  14.         for(int i = 0, n = NEEDED[H]; i < n; ++i) {
  15.           ENOUGH[H].signal();
  16.         }
  17.         COUNT[H] -= NEEDED[H];
  18.         for(int i = 0, n = NEEDED[O]; i < n; ++i) {
  19.           ENOUGH[O].signal();
  20.         }
  21.         COUNT[O] -= NEEDED[O];
  22.       }
  23.       ++COUNT[index];
  24.       ENOUGH[index].awaitUninterruptibly();
  25.     } finally {
  26.       LOCK.unlock();
  27.     }
  28.   }
  29.  
  30.   public static void h() {
  31.     run(H);
  32.   }
  33.  
  34.   public static void o() {
  35.     run(O);
  36.   }
  37. }
http://greenteapress.com/semaphores/downey05semaphores.pdf
Read full article from LinkedIn: H2O

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts