Advertisement
Josif_tepe

Untitled

Apr 3rd, 2021
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. import java.awt.geom.AffineTransform;
  2. import java.lang.invoke.SwitchPoint;
  3. import java.util.concurrent.Semaphore;
  4. import java.util.*;
  5. public class Main {
  6.     public static void main(String args[]) {
  7.         init();
  8.         List<Thread> c_atoms = new ArrayList<>();
  9.         List<Thread> h_atoms = new ArrayList<>();
  10.         List<Thread> o_atoms = new ArrayList<>();
  11.         for(int i = 0; i < 20; i++) {
  12.             c_atoms.add(new C());
  13.         }
  14.         for(int i = 0; i < 40; i++) {
  15.             h_atoms.add(new H());
  16.         }
  17.         for(int i = 0; i < 20 ;i++) {
  18.             o_atoms.add(new O());
  19.         }
  20.         for(int i = 0; i < 20 ; i++) {
  21.             c_atoms.get(i).start();
  22.         }
  23.         for(int i = 0; i < 40; i++) {
  24.             h_atoms.get(i).start();
  25.         }
  26.         for(int i = 0; i < 20 ;i++) {
  27.             o_atoms.get(i).start();
  28.         }
  29.         try {
  30.             for(int i = 0; i < 20 ; i++) {
  31.                 c_atoms.get(i).join();
  32.             }
  33.             for(int i = 0; i < 40; i++) {
  34.                 h_atoms.get(i).join();
  35.             }
  36.             for(int i = 0; i < 20 ;i++) {
  37.                 o_atoms.get(i).join();
  38.             }
  39.         }
  40.         catch (InterruptedException e) {
  41.  
  42.         }
  43.     }
  44.     static Semaphore c;
  45.     static Semaphore h;
  46.     static Semaphore o;
  47.     static Semaphore c_created;
  48.     static Semaphore h_created;
  49.     static Semaphore o_created;
  50.     static Semaphore boss;
  51.  
  52.     private static void init() {
  53.         c = new Semaphore(2);
  54.         h = new Semaphore(4);
  55.         o = new Semaphore(2);
  56.         c_created = new Semaphore(0);
  57.         h_created = new Semaphore(0);
  58.         o_created = new Semaphore(0);
  59.         boss = new Semaphore(0);
  60.     }
  61.     static class C extends Thread {
  62.  
  63.         private void excecute() throws InterruptedException {
  64.             c.acquire(1);
  65.             c_created.release(6);
  66.             h_created.acquire(1);
  67.             o_created.acquire(1);
  68.             boss.release(6);
  69.             System.out.println("C bonding");
  70.             c.release(1);
  71.         }
  72.  
  73.         @Override
  74.         public void run() {
  75.             try {
  76.                 excecute();
  77.             } catch (InterruptedException e) {
  78.  
  79.             }
  80.         }
  81.     }
  82.     static class H extends Thread {
  83.         private void excecute() throws InterruptedException{
  84.             h.acquire(1);
  85.             c_created.acquire(1);
  86.             h_created.release(4);
  87.             o_created.acquire(1);
  88.             boss.acquire(1);
  89.             System.out.println("H bonding");
  90.             h.release();
  91.         }
  92.         @Override
  93.         public void run() {
  94.             try {
  95.                 excecute();
  96.  
  97.             }
  98.             catch (InterruptedException e) {
  99.  
  100.             }
  101.         }
  102.     }
  103.     static class O extends Thread {
  104.         private void excecute() throws InterruptedException{
  105.             o.acquire(1);
  106.             c_created.acquire(1);
  107.             h_created.acquire(1);
  108.             o_created.release(6);
  109.             boss.acquire(1);
  110.             System.out.println("O bonding");
  111.             o.release(1);
  112.         }
  113.         @Override
  114.         public void run() {
  115.             try {
  116.                 excecute();
  117.             }
  118.             catch (InterruptedException e) {
  119.  
  120.             }
  121.         }
  122.     }
  123.  
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement