Monday, September 11, 2017

Sequential Display Using Semaphore and Wait Notify.

Semaphore:

A counting semaphore. Conceptually, a semaphore maintains a set of permits. Each acquire() blocks if necessary until a permit is available, and then takes it. Each release() adds a permit, potentially releasing a blocking acquirer. However, no actual permit objects are used; the Semaphore just keeps a count of the number available and acts accordingly.
Semaphores are often used to restrict the number of threads than can access some (physical or logical) resource. For example, here is a class that uses a semaphore to control access to a pool of items:

Semaphores have no notion of ownership, so they cannot be reentrant, although as many permits as are available may be acquired. That means a thread needs to block when it encounters a value of 0, until someone increments the semaphore.


// Sequential Display Using Semaphore 

package com.corejavarevise;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;


/**
 * Sequencial Printing Using Semaphore
 * @author S730175
 *
 */
public class PrintSequenceWithSemaphore {

public static void main(String[] args) {
// TODO Auto-generated method stub
final PrintSequence printSequence = new PrintSequenceWithSemaphore.PrintSequence();
ExecutorService executors = Executors.newFixedThreadPool(3);
executors.execute(new Runnable() {

@Override
public void run() {
printSequence.printOne();
}
});
executors.execute(new Runnable() {

@Override
public void run() {
printSequence.printTwo();
}
});
executors.execute(new Runnable() {

@Override
public void run() {
printSequence.printThree();
}
});

}

static class PrintSequence {
volatile int i = 0;
Semaphore sm1 = new Semaphore(1);
Semaphore sm2 = new Semaphore(0);
Semaphore sm3 = new Semaphore(0);

public void printOne() {
while(i<100) {
try {
sm1.acquire();
System.out.println(++i);
sm2.release();

} catch (InterruptedException e) {

}
}
}

public void printTwo() {
while(i<100) {
try {
sm2.acquire();
System.out.println(++i);
sm3.release();

} catch (InterruptedException e) {

}
}
}

public void printThree() {
while(i<100) {
try {
sm3.acquire();
System.out.println(++i);
sm1.release();

} catch(InterruptedException e) {

}
}
}


}

}



/////////////////// Sequential Display Using Wait Notify //////////////////////


package com.corejavarevise.threadrevise;

public class SequenceDisplay {

/**
 *
 */
static Object monitor = new Object();

static boolean one = true;
static boolean two = false;
static boolean three = false;

public static void main(String[] args) {
Thread t4 = new Thread("4"){
public void run() {
System.out.println("I am thread 4");
}
};
t4.start();

Thread t1 = new Thread(new SequenceDisplayImpl(1));
 Thread t2 = new Thread(new SequenceDisplayImpl(2));
 Thread t3 = new Thread(new SequenceDisplayImpl(3));

 Thread.currentThread().getName();
 t1.start();
 t2.start();
 t3.start();

}

static class SequenceDisplayImpl implements Runnable {

 int threadId;

 SequenceDisplayImpl(int threadId) {
  this.threadId = threadId;
 }

 public void run() {
  print();
 }

 private void print() {
  try {
   while (true) {
    Thread.sleep(500);
    synchronized (monitor) {
     if (1 == threadId) {
      if (!one) {
       monitor.wait();
      } else {
       System.out.print(threadId + " ");
       one = false;
       two = true;
       three = false;
       monitor.notifyAll();
      }
     }
     if (2 == threadId) {
      if (!two) {
       monitor.wait();
      } else {
       System.out.print(threadId + " ");
       one = false;
       two = false;
       three = true;
       monitor.notifyAll();
      }
     }
     if (3 == threadId) {
      if (!three) {
       monitor.wait();
      } else {
       System.out.print(threadId + " ");
       one = true;
       two = false;
       three = false;
       monitor.notifyAll();
      }
     }
    }
   }
  } catch (InterruptedException e) {
   e.printStackTrace();
  }

 }

}

}

<a href="https://www.sejda.com/mgl/compress-pdf-onlineCompress PDF">Compress PDF</a>