Advertisement
Josif_tepe

Untitled

Apr 2nd, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Random;
  4. import java.util.Scanner;
  5. import java.util.concurrent.Semaphore;
  6.  
  7. public class Main {
  8.     public static  void main(String[] args) throws InterruptedException{
  9.         Factory f = new Factory(2);
  10.         List<Thread> ls = new ArrayList<>();
  11.         for(int i = 0; i < 3; i++) {
  12.             ls.add(new Thread(new Runnable() {
  13.                 @Override
  14.                 public void run() {
  15.                     Random rnd = new Random();
  16.                     if(rnd.nextInt() % 2 == 0) {
  17.                         try {
  18.                             f.produce_vaccine(new String("v" + rnd.nextInt() % 10));
  19.                         }
  20.                         catch (InterruptedException ex) {
  21.  
  22.                         }
  23.                     }
  24.                     else {
  25.                         try {
  26.                             f.buy_vaccine();
  27.                         }
  28.                         catch (InterruptedException ex) {
  29.  
  30.                         }
  31.                     }
  32.                 }
  33.             }));
  34.         }
  35.         for(int i = 0; i < 3; i++) {
  36.             ls.get(i).start();
  37.         }
  38.         for(int i  =0; i < 3; i++) {
  39.             try {
  40.                 ls.get(i).join();
  41.             }
  42.             catch (InterruptedException ex) {
  43.  
  44.             }
  45.         }
  46.  
  47.     }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement