package rarinugraha;

 

import java.util.concurrent.Semaphore;

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!


order now

 

/**

 *

 * @author Rarinugraha

 */

public class MutexEx {

 

    static Semaphore semaphore = new
Semaphore(1);

 

    static class EToll extends Thread {

 

        String keran = “”;

 

        EToll(String gto) {

            this.keran = gto;

        }

 

        public void run() {

 

            try {

 

                System.out.println(keran +
” : Nyari GTO (-_-)”);

                System.out.println(keran +
” : GTO yang tersedia : ”

                        + semaphore.availablePermits());

 

                semaphore.acquire();

                System.out.println(keran +
” : Masuk GTO (* ^ ? ^)”);

 

                try {

 

                    System.out.println(keran +
” : Lagi tapping,”

                            + ” GTO yang
tersedia : ”

                            +
semaphore.availablePermits());

 

                    Thread.sleep(1000);

 

                } finally {

 

                    System.out.println(keran +
” : Selesai tapping…”);

                    semaphore.release();

                    System.out.println(keran +
” : GTO yang tersedia : ”

                            +
semaphore.availablePermits());

 

                }

 

            } catch (InterruptedException e) {

 

                e.printStackTrace();

 

            }

 

        }

 

    }

 

    public static void main(String args) {

 

        System.out.println(“Total GTO
tersedia : ”

                +
semaphore.availablePermits());

 

        EToll et1 = new
EToll(“Alpha”);

        et1.start();

 

        EToll et2 = new
EToll(“Beta”);

        et2.start();

 

        EToll et3 = new
EToll(“Gamma”);

        et3.start();

 

        EToll et4 = new
EToll(“Delta”);

        et4.start();

 

   
}

}