PDA

View Full Version : پیاده سازی سمافور در جاوا



webism
شنبه 04 خرداد 1387, 17:17 عصر
سلام
چگونه می تونم کلاس سمافور و متد wait و signal را با استفاده از صف پروسس های مسدود شده پیاده سازی کنم یعنی منظورم اینه که از wait و notify جاوا استفاده نکنم می خوام کدی شبیه به کد زیر را پیاده سازی کنم لطف کنید راهنمایی کنید:


class semaphore
{
int count;
queueType queue;

void wait(semaphore s)
{
s.count--;
if(s.count < 0)
{
place this process in s.queue;
block this process;
}
}
void signal(semaphore s)
{
s.count++;
if(s.count <= 0)
{
remove a process P from s.queue;
place process P on ready list;
}
}
} چیزی شبیه به کد زیر تو اینترنت هست که منظورم این نیست:


class Semaphore {
private int count;
public Semaphore(int n) {
this.count = n;
}
public synchronized void WAIT() {
while(count == 0) {
try {
wait();
java.util.concurre
} catch (InterruptedException e) {
//keep trying
}
}
count--;
}

public synchronized void SIGNAL() {
count++;
notify(); //alert a thread that's blocking on this semaphore
}
}