Advertisement
Josif_tepe

Untitled

Apr 10th, 2021
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.55 KB | None | 0 0
  1. import javax.swing.plaf.TableHeaderUI;
  2. import java.awt.geom.AffineTransform;
  3. import java.lang.invoke.SwitchPoint;
  4. import java.util.concurrent.Semaphore;
  5. import java.util.*;
  6. public class Vinegar {
  7.     public static void main(String args[])  throws InterruptedException{
  8.         init();
  9.         HashSet<Thread> threads = new HashSet<>();
  10.         for (int i = 0; i < 20; i++) {
  11.             threads.add(new C());
  12.             threads.add(new H());
  13.             threads.add(new H());
  14.             threads.add(new O());
  15.         }
  16.         for(int i = 0; i < 20; i++) {
  17.             threads.add(new H());
  18.         }
  19.         // run all threads in background
  20. for(Thread t : threads) {
  21.     t.start();
  22. }
  23. for(Thread t : threads) {
  24.     t.join(2000);
  25. }
  26. for(Thread t : threads) {
  27.     if(t.isAlive()) {
  28.         System.out.println("Possible deadlock!");
  29.     return;
  30.     }
  31. }
  32.         // after all of them are started, wait each of them to finish for maximum 2_000 ms
  33.  
  34.         // for each thread, terminate it if it is not finished
  35.         System.out.println("Process finished.");
  36.     }
  37.     static Semaphore c;
  38.     static Semaphore h;
  39.     static Semaphore o;
  40.     static Semaphore c_created;
  41.     static Semaphore h_created;
  42.     static Semaphore o_created;
  43.     static Semaphore boss;
  44.  
  45.     private static void init() {
  46.         c = new Semaphore(2);
  47.         h = new Semaphore(4);
  48.         o = new Semaphore(2);
  49.         c_created = new Semaphore(0);
  50.         h_created = new Semaphore(0);
  51.         o_created = new Semaphore(0);
  52.         boss = new Semaphore(0);
  53.     }
  54.     static class C extends Thread {
  55.  
  56.         private void excecute() throws InterruptedException {
  57.  
  58.  
  59.             // at most 2 atoms should print this in parallel
  60.             c.acquire(1);
  61.  
  62.             System.out.println("C here.");
  63.             c_created.release(6);
  64.             h_created.acquire(1);
  65.             o_created.acquire(1);
  66.             // after all atoms are present, they should start with the bonding process together
  67.             System.out.println("Molecule bonding.");
  68.             Thread.sleep(100);// this represent the bonding process
  69.             boss.release(6);
  70.  
  71.             System.out.println("C done.");
  72.             // only one atom should print the next line, representing that the molecule is created
  73.             c.release(1);
  74.  
  75.             System.out.println("Molecule created.");
  76.         }
  77.  
  78.         @Override
  79.         public void run() {
  80.             try {
  81.                 excecute();
  82.             } catch (InterruptedException e) {
  83.  
  84.             }
  85.         }
  86.     }
  87.     static class H extends Thread {
  88.         private void excecute() throws InterruptedException{
  89.  
  90.             // at most 4 atoms should print this in parallel
  91.             System.out.println("H here.");
  92.             h.acquire(1);
  93.  
  94.             // after all atoms are present, they should start with the bonding process together
  95.             c_created.acquire(1);
  96.             h_created.release(4);
  97.             o_created.acquire(1);
  98.             System.out.println("Molecule bonding.");
  99.             Thread.sleep(100);// this represent the bonding process
  100.             boss.acquire(1);
  101.  
  102.             System.out.println("H done.");
  103.             // only one atom should print the next line, representing that the molecule is created
  104.             h.release();
  105.  
  106.             System.out.println("Molecule created.");
  107.         }
  108.         @Override
  109.         public void run() {
  110.             try {
  111.                 excecute();
  112.  
  113.             }
  114.             catch (InterruptedException e) {
  115.  
  116.             }
  117.         }
  118.     }
  119.     static class O extends Thread {
  120.         private void excecute() throws InterruptedException{
  121.  
  122.  
  123.             // at most 2 atoms should print this in parallel
  124.             o.acquire(1);
  125.             System.out.println("O here.");
  126.             // after all atoms are present, they should start with the bonding process together
  127.  
  128.             c_created.acquire(1);
  129.             h_created.acquire(1);
  130.             o_created.release(6);
  131.             System.out.println("Molecule bonding.");
  132.             Thread.sleep(100);// this represent the bonding process
  133.             boss.acquire(1);
  134.  
  135.             System.out.println("O done.");
  136.             // only one atom should print the next line, representing that the molecule is created
  137.             o.release(1);
  138.  
  139.             System.out.println("Molecule created.");
  140.         }
  141.         @Override
  142.         public void run() {
  143.             try {
  144.                 excecute();
  145.             }
  146.             catch (InterruptedException e) {
  147.  
  148.             }
  149.         }
  150.     }
  151.  
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement