Advertisement
Josif_tepe

Untitled

Apr 3rd, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. import java.awt.geom.AffineTransform;
  2. import java.util.concurrent.Semaphore;
  3. import java.util.*;
  4. public class Main {
  5.     public static void main(String args[]) {
  6.         init();
  7.         List<Thread> si_atoms = new ArrayList<>();
  8.         List<Thread> o_atoms = new ArrayList<>();
  9.         for(int i = 0; i < 5; i++) {
  10.             si_atoms.add(new Si());
  11.         }
  12.         for(int i = 0; i < 10; i++) {
  13.             o_atoms.add(new O());
  14.         }
  15.         for(int i = 0; i < 5; i++) {
  16.             si_atoms.get(i).start();
  17.         }
  18.         for(int i = 0; i < 10; i++) {
  19.             o_atoms.get(i).start();
  20.         }
  21.         try {
  22.             for (int i = 0; i < 5; i++) {
  23.                 si_atoms.get(i).join();
  24.             }
  25.             for (int i = 0; i < 10; i++) {
  26.                 o_atoms.get(i).join();
  27.             }
  28.         }
  29.         catch (InterruptedException e) {
  30.             System.out.println(e);
  31.         }
  32.     }
  33.     static Semaphore si;
  34.     static Semaphore o;
  35.     static Semaphore si_created;
  36.     static Semaphore o_created;
  37.     static Semaphore boss;
  38.     private static void init() {
  39.         si = new Semaphore(1);
  40.         o = new Semaphore(2);
  41.         si_created = new Semaphore(0);
  42.         o_created = new Semaphore(0);
  43.         boss = new Semaphore(0);
  44.     }
  45.     static class Si extends Thread {
  46.         public void bond() {
  47.             System.out.println("Si is bonding");
  48.         }
  49.         private void go() throws InterruptedException {
  50.             si.acquire();
  51.             si_created.release(2);
  52.             o_created.acquire(2);
  53.             boss.release(2);
  54.             bond();
  55.             si.release();
  56.         }
  57.         @Override
  58.         public void run() {
  59.             try {
  60.                 go();
  61.             }
  62.             catch (InterruptedException e) {
  63.                 System.out.println(e);
  64.             }
  65.         }
  66.  
  67.     }
  68.     static class O extends Thread {
  69.         public void bond() {
  70.             System.out.println("O is bonding");
  71.         }
  72.         private void go() throws InterruptedException {
  73.             o.acquire();
  74.             si_created.acquire(1);
  75.             o_created.release(1);
  76.             boss.acquire(1);
  77.             bond();
  78.             o.release();
  79.         }
  80.         @Override
  81.         public void run() {
  82.             try {
  83.                 go();
  84.             }
  85.             catch (InterruptedException e) {
  86.                 System.out.println(e);
  87.             }
  88.         }
  89.     }
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement