public class cyclicBarrier {
private static int n;
private static int count;
private static semaphore mutex;
private static semaphore turnstile;
private static semaphore turnstile2;
public cyclicBarrier(int n){
this.n = n;
this.count = 0;
this.mutex = new semaphore(1);
this.turnstile = new semaphore(0);
this.turnstile2 = new semaphore(0);
}
public synchronized void down() throws InterruptedException{
this.phase1();
this.phase2();
}
private synchronized void phase1() throws InterruptedException {
this.mutex.down();
this.count++;
if (this.count == this.n){
for (int i=0; i< this.n; i++){
this.turnstile.signal();
}
}
this.mutex.signal();
this.turnstile.down();
}
private synchronized void phase2() throws InterruptedException {
this.mutex.down();
this.count--;
if (this.count == 0){
for (int i=0; i< this.n; i++){
this.turnstile2.signal();
}
}
this.mutex.signal();
this.turnstile2.down();
}
}
&& here is class semaphore just in case
public class semaphore{
private int counter;
public semaphore(int number){
if (number > 0) {
this.counter = number;
}
}
public synchronized void signal(){
this.counter++;
notifyAll();
}
public synchronized void down() throws InterruptedException{
while (this.counter <= 0){
wait();
}
this.counter--;
}
}
This is a code that I wrote to implement Cyclicbarriers using threads. I took the pseudo code from the book along with the notes on deadlocks so I think its pretty much ok "there might be a bug though". The first phase is for "arriving threads" and the 2nd phase is for "running threads in the critical region together". My question is as follows... how to change the code in order to consider specific types of threads? For instance, there are hydrogen threads and oxygen threads and I want them to execute bond() everytime there are 2 hydrogen atoms and 1 oxygen atom at the barrier. Thank you in advance.