Advertisement
brsjak

OS - Vinegar - Januari 2020

Jan 24th, 2020
2,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.61 KB | None | 0 0
  1. /*
  2.  * Се прави киселина C2H4O2 од два атоми на C, четири атоми на H и два атоми на O.
  3.  * Да се синхронизира процесот на правење на киселина каде што треба да се ограничи влезот на атомите од секој елемент.
  4.  * При влез, атомите печатат C/H/O here. Атомите треба да чекаат додека влезат потребниот број на атоми од секој елемент.
  5.  * Откако ќе влезат потребните атоми спојувањето може да започне истовремено кај сите елементи со печатење на Module Bonding и се повикува Thread.sleep(100).
  6.  * Откако атомотите ќе завршат со спојување, секој од нив треба да испечати C/H/O done, а само еден од нив Module Created.
  7.  * После успешното спојување процесот може да започне од почеток.
  8.  *
  9. */
  10.  
  11. import java.util.HashSet;
  12. import java.util.concurrent.Semaphore;
  13.  
  14. public class Vinegar {
  15.     static Semaphore cEnter = new Semaphore(2);
  16.     static Semaphore hEnter = new Semaphore(4);
  17.     static Semaphore oEnter = new Semaphore(2);
  18.     static int count = 0;
  19.     static Semaphore lock = new Semaphore(1);
  20.     static Semaphore canBond = new Semaphore(0);
  21.    
  22.     static class C extends Thread{
  23.        
  24.         public void execute() throws InterruptedException {
  25.             cEnter.acquire();
  26.            
  27.             lock.acquire();
  28.             count++;
  29.             System.out.println("C here.");
  30.             if(count==8) {
  31.                 canBond.release(8);
  32.             }
  33.             lock.release();
  34.            
  35.             canBond.acquire();
  36.            
  37.             System.out.println("Module Bonding.");
  38.             Thread.sleep(100);
  39.            
  40.             lock.acquire();
  41.             System.out.println("C done.");
  42.             --count;
  43.             if(count==0) {
  44.                 System.out.println("Module Created.");
  45.                 //glupava proverka
  46.                 if(cEnter.availablePermits() == 0 && hEnter.availablePermits() == 0 && oEnter.availablePermits() == 0) {
  47.                     cEnter.release(2);
  48.                     hEnter.release(4);
  49.                     oEnter.release(2);
  50.                 }
  51.             }
  52.             lock.release();
  53.         }
  54.        
  55.         public void run() {
  56.             try {
  57.                 execute();
  58.             }catch(Exception e) {
  59.                 e.printStackTrace();
  60.             }
  61.         }
  62.        
  63.     }
  64.    
  65.     static class H extends Thread{
  66.         public void execute() throws InterruptedException {
  67.             hEnter.acquire();
  68.            
  69.             lock.acquire();
  70.             count++;
  71.             System.out.println("H here.");
  72.             if(count==8) {
  73.                 canBond.release(8);
  74.             }
  75.             lock.release();
  76.            
  77.             canBond.acquire();
  78.            
  79.             System.out.println("Module Bonding.");
  80.             Thread.sleep(100);
  81.            
  82.             lock.acquire();
  83.             System.out.println("H done.");
  84.             --count;
  85.             if(count==0) {
  86.                 System.out.println("Module Created.");
  87.                 //glupava proverka
  88.                 if(cEnter.availablePermits() == 0 && hEnter.availablePermits() == 0 && oEnter.availablePermits() == 0) {
  89.                     cEnter.release(2);
  90.                     hEnter.release(4);
  91.                     oEnter.release(2);
  92.                 }
  93.             }
  94.             lock.release();
  95.         }
  96.        
  97.         public void run() {
  98.             try {
  99.                 execute();
  100.             }catch(Exception e) {
  101.                 e.printStackTrace();
  102.             }
  103.         }
  104.     }
  105.    
  106.     static class O extends Thread{
  107.         public void execute() throws InterruptedException {
  108.             oEnter.acquire();
  109.             lock.acquire();
  110.             count++;
  111.             System.out.println("O here.");
  112.             if(count==8) {
  113.                 canBond.release(8);
  114.             }
  115.             lock.release();
  116.             canBond.acquire();
  117.            
  118.             System.out.println("Module Bonding.");
  119.             Thread.sleep(100);
  120.            
  121.             lock.acquire();
  122.             System.out.println("O done.");
  123.             --count;
  124.             if(count==0) {
  125.                 System.out.println("Module Created.");
  126.                 //glupava proverka
  127.                 if(cEnter.availablePermits() == 0 && hEnter.availablePermits() == 0 && oEnter.availablePermits() == 0) {
  128.                     cEnter.release(2);
  129.                     hEnter.release(4);
  130.                     oEnter.release(2);
  131.                 }
  132.             }
  133.             lock.release();
  134.         }
  135.        
  136.         public void run() {
  137.             try {
  138.                 execute();
  139.             }catch(Exception e) {
  140.                 e.printStackTrace();
  141.             }
  142.         }
  143.     }
  144.    
  145.     public static void main(String[] args) throws InterruptedException {
  146.         // TODO Auto-generated method stub
  147.         HashSet <Thread> threads = new HashSet<>();
  148.        
  149.         for(int i=0;i<30;i++) {
  150.             C c = new C();
  151.            
  152.             O o = new O();
  153.            
  154.             threads.add(c);
  155.            
  156.             threads.add(o);
  157.         }
  158.         for(int i=0;i<60;i++) {
  159.             H h = new H();
  160.             threads.add(h);
  161.         }
  162.        
  163.         for(Thread t : threads) {
  164.             t.start();
  165.         }
  166.        
  167.         for(Thread t : threads) {
  168.             t.join(2000);
  169.         }
  170.         for(Thread t : threads) {
  171.             if(t.isAlive()) {
  172.                 t.interrupt();
  173.                 System.out.println("Possible Deadlock!");
  174.             }
  175.         }
  176.         System.out.println("Process Finished.");
  177.     }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement