Guest User

Untitled

a guest
Oct 24th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. public class Sito implements Runnable {
  2.    
  3.     int id;
  4.    
  5.    
  6.     public Sito(int id) {
  7.         this.id = id;
  8.         System.out.println("Witam, jestem watek nr: " + id );
  9.     }
  10.  
  11.     @Override
  12.     public void run() {
  13.        
  14.        
  15.             int i = 1;
  16.             try {
  17.                
  18.                 while(i<Main.maximum)
  19.                 {
  20.                    
  21.                     System.out.println(id + " probuje uzyskac dostep(PPP)");
  22.                     Main.semaphore.acquire();
  23.                     System.out.println(id + " uzyskal dostep(PPP)");
  24.                     i = Main.set.nextSetBit(i + 1);
  25.                     Main.semaphore.release();
  26.                     System.out.println(id + " zwolnil dostep(PPP)");
  27.  
  28.                     for (int j = i*2; j < Main.size; j += i)
  29.                     {
  30.                         System.out.println(id + " probuje uzyskac dostep(DDD)");
  31.                         Main.semaphore.acquire();
  32.                         System.out.println(id + " uzyskal dostep(DDD)");
  33.                         if(Main.set.get(i)) {
  34.                             Main.set.clear(j);
  35.                             Main.semaphore.release();
  36.                             System.out.println(id + " zwolnil dostep(DDD)");
  37.                         }
  38.                         else {
  39.                             Main.semaphore.release();
  40.                             System.out.println(id + " zwolnil dostep(DDD)");
  41.                             break;
  42.                         }
  43.                     }
  44.                    
  45.                 }
  46.                
  47.             } catch (InterruptedException e) {
  48.                 // TODO Auto-generated catch block
  49.                 e.printStackTrace();
  50.             }
  51.            
  52.                
  53.                
  54.            
  55.        
  56.        
  57.     }
  58.    
  59.     private int getNextToCheck(int next) throws InterruptedException {
  60.         Main.semaphore.acquire();
  61.         int z=  Main.set.nextSetBit(next + 1);
  62.         Main.semaphore.release();
  63.         return z;
  64.        
  65.     }
  66.    
  67.     private boolean clearIfNeeded(int i,int j) throws InterruptedException
  68.     {
  69.         Main.semaphore.acquire();
  70.        
  71.         if(Main.set.get(i)==false)
  72.             {
  73.             Main.semaphore.release();
  74.             return false;
  75.             }
  76.         else {
  77.             Main.set.clear(j);
  78.             Main.semaphore.release();
  79.             return true;
  80.            
  81.         }
  82.        
  83.        
  84.     }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment