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 SemaphoreEx {

 

    static Semaphore semaphore = new
Semaphore(2);

 

    static class TempatWudhu extends Thread {

 

        String keran = “”;

 

        TempatWudhu(String keran) {

            this.keran = keran;

        }

 

        public void run() {

 

            try {

 

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

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

                        +
semaphore.availablePermits());

 

                semaphore.acquire();

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

 

                try {

 

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

                            + ” keran yang
tersedia : ”

                            +
semaphore.availablePermits());

 

                    Thread.sleep(1000);

 

                } finally {

 

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

                    semaphore.release();

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

                            +
semaphore.availablePermits());

 

                }

 

            } catch (InterruptedException e) {

 

                e.printStackTrace();

 

            }

 

        }

 

    }

 

    public static void main(String args) {

 

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

                +
semaphore.availablePermits());

 

        TempatWudhu tw1 = new
TempatWudhu(“Alpha”);

        tw1.start();

 

        TempatWudhu tw2 = new
TempatWudhu(“Beta”);

        tw2.start();

 

        TempatWudhu tw3 = new
TempatWudhu(“Gamma”);

        tw3.start();

 

        TempatWudhu tw4 = new
TempatWudhu(“Delta”);

        tw4.start();

 

   
}

}

Post Author: admin

x

Hi!
I'm Eileen!

Would you like to get a custom essay? How about receiving a customized one?

Check it out