Guest User

Untitled

a guest
May 22nd, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class CThread extends Thread
  4. {
  5.     Type type;
  6.     Direction dir;
  7.     Manager m;
  8.    
  9.     public CThread(Manager m)
  10.     {
  11.         this.m = m;
  12.         Random rand = new Random();
  13.         int type = rand.nextInt(2);
  14.        
  15.         if(type == 0)
  16.         {
  17.             this.type = Type.SLIM;
  18.         }
  19.         else
  20.         {
  21.             this.type = Type.FAT;
  22.         }
  23.        
  24.         int direction = rand.nextInt(2);
  25.        
  26.         if(direction == 0)
  27.         {
  28.             this.dir = Direction.NORTH;
  29.         }
  30.         else
  31.         {
  32.             this.dir = Direction.SOUTH;
  33.         }
  34.     }
  35.    
  36.     public void run()
  37.     {
  38.         m.Acquire(this);
  39.         m.Simulate(2000);
  40.         m.Release(this);       
  41.     }
  42.    
  43.     public static void main(String[] args) throws InterruptedException
  44.     {
  45.         Manager m = new Manager();
  46.         CThread[] vehicles = new CThread[30];
  47.         for(int i = 0 ; i < 30; i++)
  48.         {
  49.             vehicles[i] = new CThread(m);
  50.             vehicles[i].start();
  51.         }
  52.        
  53.         for(int i = 0 ; i < 30; i++)
  54.         {
  55.             vehicles[i].join();
  56.         }
  57.     }
  58. }
Add Comment
Please, Sign In to add comment