Guest User

Our BTL

a guest
Aug 21st, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. tester.java
  2. =======================
  3. import java.util.Random;
  4. public class Tester {
  5.  
  6.     public static void main(String[] args) {
  7.        
  8.         Random rnd=new Random();
  9.        
  10.         final int TOTAL_VICTIMS=rnd.nextInt(50)+50;
  11.        
  12.         //create random number of customers
  13.         Customer[] btlCust = new Customer[TOTAL_VICTIMS];
  14.         for (int counter=0;counter<btlCust.length;counter+=1)
  15.         {
  16.             btlCust[counter]=new Customer();
  17.             btlCust[counter].start();
  18.         }
  19.         System.out.println("Starting to serve");
  20.         Pakid[] pkidim = new Pakid[5];
  21.         for (int counter=0;counter<pkidim.length;counter+=1)
  22.         {
  23.             pkidim[counter]=new Pakid();
  24.             pkidim[counter].start();
  25.            
  26.         }
  27.     }
  28.  
  29. }
  30.  
  31.  
  32.  
  33. LocalBtl.java
  34. ===================
  35.  
  36. public class LocalBtl {
  37.     private static int nextInLine=0;
  38.    
  39.     public static int incLine()
  40.     {
  41.         nextInLine+=1;
  42.         return nextInLine;
  43.     }
  44.    
  45.     public static int getNextInLine()
  46.     {
  47.         return nextInLine;
  48.     }
  49. }
  50.  
  51. Pakid.java
  52. ==============
  53. import java.util.Random;
  54.  
  55. public class Pakid extends Thread {
  56.     private static int pakidNum=0;
  57.     private int localPakidNum;
  58.     boolean isOpen = true;
  59.     Random rnd = new Random();
  60.    
  61.     public Pakid()
  62.     {
  63.         pakidNum+=1;
  64.         localPakidNum=pakidNum;
  65.         System.out.println("Pakid number #"+localPakidNum+" was created");
  66.     }
  67.    
  68.     @Override
  69.     public synchronized void run() {
  70.         // TODO Auto-generated method stub
  71.         while (isOpen) {
  72.             while (LocalBtl.getNextInLine()<Customer.totalCustomers()) {
  73.                 System.out.println("Pakid #"+localPakidNum+" is Ready to serve");
  74.                 LocalBtl.incLine();
  75.                
  76.                 long timeToServe = rnd.nextInt(500)+500;
  77.                 try {
  78.                     Thread.sleep(timeToServe);
  79.                     System.out.println("Customer#"+LocalBtl.getNextInLine()+" was served :"+timeToServe+"ms");
  80.                 } catch (InterruptedException e) {
  81.                     // TODO Auto-generated catch block
  82.                     e.printStackTrace();
  83.                 }
  84.                
  85.             }
  86.             try {
  87.                 System.out.println("Pakid #"+localPakidNum+">Do not have any customer, me drinking some tea");
  88.                 Thread.sleep(5000);
  89.                 closeEmda();
  90.             } catch (InterruptedException e) {
  91.                 // TODO Auto-generated catch block
  92.                 e.printStackTrace();
  93.             }
  94.         }
  95.         System.out.println("Pakid #"+localPakidNum+">i finished, have a nice day :)");
  96.     }
  97.  
  98.     public void closeEmda() {
  99.         this.isOpen = false;
  100.     }
  101.  
  102. }
  103.  
  104.  
  105. Customer.java
  106. =================
  107.  
  108. public class Customer extends Thread {
  109.     private static int getNumber=0;
  110.     private int custNum;   
  111.    
  112.     public Customer()
  113.     {
  114.         getNumber+=1;
  115.         this.custNum=getNumber;
  116.         System.out.println("Customer #"+custNum+" created.");
  117.     }
  118.     @Override
  119.     public synchronized void run() {
  120.         System.out.println("Customer #"+custNum+" is waiting");
  121.         while (this.custNum>LocalBtl.getNextInLine())
  122.         {
  123.             try {
  124.                 wait();
  125.             } catch (InterruptedException e) {
  126.                 // TODO Auto-generated catch block
  127.                 e.printStackTrace();
  128.             }
  129.         }
  130.        
  131.         System.out.println("customer #"+this.custNum+" was served, thank you very clatch!");
  132.     }
  133.     public static int totalCustomers()
  134.     {
  135.         return getNumber;
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment