Semaphoreとsynchronizedの違い

1410 ワード

Semaphoreとsynchronizedの最も主要な違いは、Semaphoreが複数の同時を指定できることですが、synchronizedは1つしかありません!
 
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

public class SemaphoreTest {
	public static void main(String[] args) {
		ExecutorService service = Executors.newCachedThreadPool();
		final  Semaphore sp = new Semaphore(3);
		for(int i=0;i<10;i++){
			Runnable runnable = new Runnable(){
					public void run(){
					try {
						sp.acquire();
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
					System.out.println(" " + Thread.currentThread().getName() + 
							" , " + (3-sp.availablePermits()) + " ");
					try {
						Thread.sleep((long)(Math.random()*10000));
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(" " + Thread.currentThread().getName() + 
							" ");					
					sp.release();
					// , 
					System.out.println(" " + Thread.currentThread().getName() + 
							" , " + (3-sp.availablePermits()) + " ");					
				}
			};
			service.execute(runnable);			
		}
	}

}