Leetcode Submit Failed Please Try Again

Problem statement –

Building H2O is an interisting concurrency problem, here nosotros need to use 2 or more concepts of concurrent programming. Lets get-go understand the problem,

There are two kinds of threads,oxygen andhydrogen. Your goal is to group these threads to form water molecules. In that location is a barrier where each thread has to wait until a complete molecule can be formed. Hydrogen and oxygen threads will be givenreleaseHydrogen andreleaseOxygen methods respectively, which will permit them to laissez passer the barrier. These threads should pass the barrier in groups of 3, and they must be able to immediately bond with each other to form a water molecule. You must guarantee that all the threads from one molecule bailbefore whatsoever other threads from the next molecule do.

In other words:

  • If an oxygen thread arrives at the barrier when no hydrogen threads are present, it has to wait for ii hydrogen threads.
  • If a hydrogen thread arrives at the bulwark when no other threads are present, it has to wait for an oxygen thread and another hydrogen thread.

We don't accept to worry about matching the threads up explicitly; that is, the threads do non necessarily know which other threads they are paired up with. The key is only that threads laissez passer the barrier in complete sets; thus, if we examine the sequence of threads that bail and separate them into groups of three, each grouping should contain ane oxygen and 2 hydrogen threads.

Write synchronization code for oxygen and hydrogen molecules that enforces these constraints.

Example one:

                Input: "HOH" Output: "HHO" Explanation: "HOH" and "OHH" are also valid answers.                              

Example 2:

                Input: "OOHHHH" Output: "HHOHHO" Explanation: "HOHHHO", "OHHHHO", "HHOHOH", "HOHHOH", "OHHHOH", "HHOOHH", "HOHOHH" and "OHHOHH" are also valid answers.                              

Constraints:

  • Total length of input string will be 3north, where 1 ≤north ≤ 20.
  • Total number ofH will be 2due north in the input cord.
  • Full number ofO volition benorthward in the input string.

Solution

                
class H2o {      private Semaphore hydrogen = new Semaphore(2);     private Semaphore oxygen = new Semaphore(1);     private CyclicBarrier cb = new CyclicBarrier(3);          public void hydrogen(Runnable releaseHydrogen) throws InterruptedException { 		         // releaseHydrogen.run() outputs "H". Do not change or remove this line.         attempt{             hydrogen.larn();              releaseHydrogen.run();              cb.await();             hydrogen.release();         }grab(Exception e){                      }              }      public void oxygen(Runnable releaseOxygen) throws InterruptedException {                  // releaseOxygen.run() outputs "O". Practise not change or remove this line.         try{             oxygen.acquire();              releaseOxygen.run();              cb.look();             oxygen.release();         }take hold of(Exception e){                      }     } }                

Explanation

  1. On initialization of class Water, nosotros create and initialize 2 Semaphores and ane CyclicBarrier equally mentioned below
  2. hydrogen – initialized with permit of 2 (as we need to print H twice).
  3. oxygen – initialized with permit of 1 (cause nosotros need to print O once).
  4. cb – initialized with parties set to three ( We need cyclic barrier because we want to wait till two hydrogen and one oxygen is printed).
  5. Lets consider below three scenarios,
  6. HH – Commencement 2 threads call hydrogen(Runnable releaseHydrogen) method, every bit we take 2 permits in hydrogen semaphore both threads volition go access and print HH and call cb.look() (cb is not even so broken equally cb is left with i political party). Now, no other thread can get permit to hydrogen semaphore considering let is 0 and cb volition but be broken when one more thread calls cb.look() which means only oxygen thread tin break cb. Hence, we will print HHO.
  7. OO – Get-go 2 threads call oxygen(Runnable releaseOxygen) method, every bit we have 1 permit for oxygen semaphore, one thread will become access to oxygen and another thread will be waiting at oxygen.aquire(). First thread who got access will print O and execute cb.expect() (decrementing value of parties to 2). This thread can not go along further due to cyclic barrier. Another thread for O is waiting at oxygen.aquire(). method, every bit we have i But style by which cb could be cleaved is when 2 hydrogen threads executes. Thus, press OHH. Other oxygen thread would then get admission to semaphore and will be print will some other batch of H2O.
  8. OH/ HO – Showtime 2 threads are either OH or HO, execution volition be mixer of above 2 scenarios. one gets access to oxygen semaphore and another gets access to hydrogen semaphore. Thus, press OH or HO and cb volition waiting to exist cleaved by one H thread. thus, at the end would impress OHH or HOH.
  9. Higher up process will exist repeaded till the input ends.

Please allow me know if you accept whatsoever questions by calculation a annotate below.

jonesfoor1950.blogspot.com

Source: https://distinguisheddeveloper.wordpress.com/2020/09/20/leetcode-concurrency-building-h2o-solution/

0 Response to "Leetcode Submit Failed Please Try Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel