Advertisement
Guest User

Untitled

a guest
Oct 25th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 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 =2;
  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.                     if(Main.nextl > 0 ) i = Main.nextl;
  25.                     Main.nextl = Main.set.nextSetBit((Main.nextl) + 1);
  26.                     //if(Main.nextl<0) break;
  27.                     Main.semaphore.release();
  28.                     //System.out.println(id + " zwolnil dostep(PPP)");
  29.                     //System.out.println(" i:" + i);
  30.                     for (int j = i*2; j < Main.size; j += i)
  31.                     {
  32.                         //System.out.println(id + " probuje uzyskac dostep(DDD)");
  33.                         Main.semaphore.acquire();
  34.                         //System.out.println(id + " uzyskal dostep(DDD)");
  35.                         if(Main.set.get(j)) {
  36.                             Main.set.clear(j);
  37.                             Main.semaphore.release();
  38.                             //System.out.println("i: " + i + ", j: "+ j );
  39.                             //System.out.println(id + " zwolnil dostep(DDD)");
  40.                         }
  41.                         else {
  42.                             Main.semaphore.release();
  43.                         }
  44.                     }
  45.                    
  46.                 }
  47.                
  48.             } catch (InterruptedException e) {
  49.                 // TODO Auto-generated catch block
  50.                 e.printStackTrace();
  51.             }
  52.            
  53.                
  54.                
  55.            
  56.        
  57.        
  58.     }
  59.    
  60.     @SuppressWarnings("unused")
  61.     private int getNextToCheck(int next) throws InterruptedException {
  62.         Main.semaphore.acquire();
  63.         int z=  Main.set.nextSetBit(next + 1);
  64.         Main.semaphore.release();
  65.         return z;
  66.        
  67.     }
  68.    
  69.     @SuppressWarnings("unused")
  70.     private boolean clearIfNeeded(int i,int j) throws InterruptedException
  71.     {
  72.         Main.semaphore.acquire();
  73.        
  74.         if(Main.set.get(i)==false)
  75.             {
  76.             Main.semaphore.release();
  77.             return false;
  78.             }
  79.         else {
  80.             Main.set.clear(j);
  81.             Main.semaphore.release();
  82.             return true;
  83.            
  84.         }
  85.        
  86.        
  87.     }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement