Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Sito implements Runnable {
- int id;
- public Sito(int id) {
- this.id = id;
- //System.out.println("Witam, jestem watek nr: " + id );
- }
- @Override
- public void run() {
- int i =2;
- try {
- while(i<Main.maximum)
- {
- //System.out.println(id + " probuje uzyskac dostep(PPP)");
- Main.semaphore.acquire();
- //System.out.println(id + " uzyskal dostep(PPP)");
- if(Main.nextl > 0 ) i = Main.nextl;
- Main.nextl = Main.set.nextSetBit((Main.nextl) + 1);
- //if(Main.nextl<0) break;
- Main.semaphore.release();
- //System.out.println(id + " zwolnil dostep(PPP)");
- //System.out.println(" i:" + i);
- for (int j = i*2; j < Main.size; j += i)
- {
- //System.out.println(id + " probuje uzyskac dostep(DDD)");
- Main.semaphore.acquire();
- //System.out.println(id + " uzyskal dostep(DDD)");
- if(Main.set.get(j)) {
- Main.set.clear(j);
- Main.semaphore.release();
- //System.out.println("i: " + i + ", j: "+ j );
- //System.out.println(id + " zwolnil dostep(DDD)");
- }
- else {
- Main.semaphore.release();
- }
- }
- }
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @SuppressWarnings("unused")
- private int getNextToCheck(int next) throws InterruptedException {
- Main.semaphore.acquire();
- int z= Main.set.nextSetBit(next + 1);
- Main.semaphore.release();
- return z;
- }
- @SuppressWarnings("unused")
- private boolean clearIfNeeded(int i,int j) throws InterruptedException
- {
- Main.semaphore.acquire();
- if(Main.set.get(i)==false)
- {
- Main.semaphore.release();
- return false;
- }
- else {
- Main.set.clear(j);
- Main.semaphore.release();
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement