Advertisement
Guest User

filozof.java

a guest
Oct 27th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. package lab4Filo;
  2.  
  3. public class Filozof extends Thread{
  4.     Bat stg,dr;
  5.     int id;
  6.     public Filozof(Bat st, Bat dr,int id){
  7.         this.stg = st;
  8.         this.dr = dr;
  9.         this.id = id;
  10.     }
  11.     public void run(){
  12.         while(true){
  13.             if(stg.bat == false || dr.bat == false){
  14.                 synchronized(stg){
  15.                     try{
  16.                         System.out.println("Filozoful " + id +" asteapta!");
  17.                         sleep(700);
  18.                        
  19.                     }
  20.                     catch(InterruptedException e)
  21.                     {
  22.                         e.printStackTrace();
  23.                     }
  24.                 }
  25.                 synchronized(dr){
  26.                     try{
  27.                         //System.out.println("Filozoful " + id +" asteapta!");
  28.                         sleep(400);
  29.                        
  30.                     }
  31.                     catch(InterruptedException e)
  32.                     {
  33.                         e.printStackTrace();
  34.                     }
  35.                 }
  36.             }
  37.            
  38.             if(dr.bat == true){
  39.                 dr.bat = false;
  40.                 synchronized(dr){
  41.                     if(stg.bat == true){
  42.                         stg.bat = false;
  43.                         synchronized(stg){
  44.                             try{
  45.                                 sleep(1000);
  46.                             }
  47.                             catch(InterruptedException e){
  48.                                 e.printStackTrace();
  49.                             }
  50.                             System.out.println("Filozoful " + id +" a mancat!");
  51.                             dr.bat = true;
  52.                             stg.bat = true;
  53.                             System.out.println("Filozoful " + id +" mediteaza!");
  54.                             try{
  55.                                 sleep(1000);
  56.                             }
  57.                             catch(InterruptedException e){
  58.                                 e.printStackTrace();
  59.                             }
  60.                         }
  61.                     }
  62.                 }
  63.                
  64.             }
  65.            
  66.            
  67.         }
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement