Advertisement
Martina312

Untitled

Apr 2nd, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. import java.util.concurrent.Semaphore;
  2.  
  3. class H2OMachine {
  4.  
  5.     String[] molecule;
  6.     int count;
  7.  
  8.     static Semaphore h;
  9.     static Semaphore o;
  10.     static Semaphore hHere;
  11.     static Semaphore oHere;
  12.     static Semaphore lock;
  13.  
  14.     public H2OMachine() {
  15.         molecule = new String[3];
  16.         count = 0;
  17.         h = new Semaphore(2);
  18.         o=new Semaphore(1);
  19.         hHere = new Semaphore(0);
  20.         oHere = new Semaphore(0);
  21.         lock = new Semaphore(0);
  22.     }
  23.  
  24.     public void hydrogen() throws InterruptedException {
  25.         // TODO: 3/29/20 synchronized logic here
  26.         h.acquire();
  27.         oHere.acquire(); //ceka info dali stignal O
  28.         hHere.release();
  29.         lock.acquire();
  30.         System.out.println("The molecule is formed");
  31.         h.release();
  32.  
  33.     }
  34.  
  35.     public void oxygen() throws InterruptedException {
  36.         // TODO: 3/29/20 synchronized logic here
  37.         o.acquire(); //od pollot se vadi element od kislorod
  38.         oHere.release(2); //ke bide koordinator, znaci ke izvesti 2 h atomi deka pristignal
  39.         hHere.acquire(2); //ceka da doznae dali i dva H atomi pristignale
  40.         lock.release(2);
  41.         System.out.println("The molecule is formed");
  42.         o.release();
  43.     }
  44. }
  45. class H2OThread extends Thread {
  46.  
  47.     H2OMachine molecule;
  48.     String atom;
  49.  
  50.     public H2OThread(H2OMachine molecule, String atom){
  51.         this.molecule = molecule;
  52.         this.atom = atom;
  53.     }
  54.  
  55.     public void run() {
  56.         if ("H".equals(atom)) {
  57.             try {
  58.                 molecule.hydrogen();
  59.             }
  60.             catch (Exception e) {
  61.             }
  62.         }
  63.         else if ("O".equals(atom)) {
  64.             try {
  65.                 molecule.oxygen();
  66.             }
  67.             catch (Exception e) {
  68.             }
  69.         }
  70.     }
  71. }
  72.  
  73. public class Main
  74. {
  75.     public static void main(String[] args) {
  76.  
  77.         // TODO: 3/29/20 Simulate with multiple scenarios
  78.         H2OMachine molecule = new H2OMachine();
  79.  
  80.         Thread t1 = new H2OThread(molecule,"H");
  81.         Thread t2 = new H2OThread(molecule,"O");
  82.         Thread t3 = new H2OThread(molecule,"H");
  83.         Thread t4 = new H2OThread(molecule,"O");
  84.  
  85.         t2.start();
  86.         t1.start();
  87.         t4.start();
  88.         t3.start();
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement