Explaining Semaphore by example

     This post helps to explore more into java.util.concurrent package. It explains java.util.concurrent.Semaphore class with example. Many time while developing multi threaded application , we may have multiple threads accessing particular system resource concurrently. If you want to limit no of open request for the resource then Semaphore is the option available in java.
      Limiting no of open request we can improve throughout of a systemn. Using Semaphore we can control no of running thread at a time out of all available running threads.

Following example explains using Semaphore

  1: import java.util.*;
  2: import java.util.concurrent.*;
  3: class SemaphoreTest 
  4: {
  5: 	public static void main(String[] args) 
  6: 	{
  7: 		// Limiting No on threads running to 2
  8: 		Semaphore semaphore = new Semaphore(2);
  9: 		for(int i=0 ;i<5;i++){
 10: 			new MyThread(String.valueOf(i),semaphore).start();
 11: 		}
 12: 		System.out.println("End of Semaphore Test");
 13: 	}
 14: }
 15: 
 16: 
 17: class MyThread extends Thread
 18: {
 19: 	private String name  = null;	
 20: 	private Semaphore semaphore =null;
 21: 	public MyThread(String name,Semaphore semaphore){
 22: 		this.name = name;
 23: 		this.semaphore = semaphore;
 24: 	}
 25: 
 26: 	public void run(){
 27: 		try{
 28: 		semaphore.acquire();
 29: 		System.out.println("Thread "+ name +"is start running");
 30: 		sleep(500);
 31: 		semaphore.release();
 32: 		System.out.println("Thread "+ name +" Ends");
 33: 		}catch(Exception exp){
 34: 			exp.printStackTrace();
 35: 		}
 36: 	}
 37: 
 38: }

In this example, i have created 10 threads but no of running threads are limited to only 2, see line on 7. Semaphore semaphore = new Semaphore(2);


this semaphore is being used in MyThread to control no of running threads as shown in line no 28 & 32.

  1: semaphore.acquire();
  2: semaphore.release();

Lets have look at output of this code


---------- java ----------
Thread 0 is start running
Thread 1 is start running
End of Semaphore Test
Thread 0 Ends
Thread 1 Ends
Thread 4 is start running
Thread 2 is start running
Thread 2 Ends
Thread 4 Ends
Thread 3 is start running
Thread 3 Ends

Output completed (1 sec consumed)

Here from this output trace, we can see only two three threads are running at a time. First thread 0 & 1 have started and Thread 4 & 2 has started only when Thread 0 & 1 ended.

Now lets see by allowing 3 threads run at a time, only change we have to make is at code line no 7.


Semaphore semaphore = new Semaphore(3);


Now output is :

---------- java ----------
Thread 0 is start running
Thread 1 is start running
Thread 2 is start running
End of Semaphore Test
Thread 0 Ends
Thread 4 is start running
Thread 2 Ends
Thread 3 is start running
Thread 1 Ends
Thread 4 Ends
Thread 3 Ends

Output completed (1 sec consumed)

Here we can notice three threads got chance to run at a time.


Reference: http://www.ibm.com/developerworks/java/library/j-5things5.html?ca=drs-

Comments

Popular posts from this blog

State Design Pattern by Example

Eclipse command framework core expression: Property tester

Composite Design Pattern by example