본문 바로가기

카테고리 없음

쓰레드 Semaphore

공유 자원 접근 설정

this.m_sem = new Semaphore(num);


공유 자원 취득 ( 공유 자원이 없을 경우 해제 될때까지 기다린다. )

this.m_sem.acquire();


공유 자원 해제

this.m_sem.release();


샘플)

class SemaphoreObject

{

private Semaphore m_sem;

public SemaphoreObject(int num)

{

this.m_sem = new Semaphore(num);

}


public void semGet()throws InterruptedException

{

this.m_sem.acquire();

System.out.println(this.m_sem.availablePermits());

}


public void semRelease()throws InterruptedException

{

this.m_sem.release();

System.out.println(this.m_sem.availablePermits());

}

}