开发者

semaphores in java

开发者 https://www.devze.com 2023-02-15 03:05 出处:网络
Has anyone got any idea how to i开发者_高级运维mplement a rudimentary semaphore in java without making use of wait(), notify() or synchronize.I am not looking for a solution to this problem just a poi

Has anyone got any idea how to i开发者_高级运维mplement a rudimentary semaphore in java without making use of wait(), notify() or synchronize.I am not looking for a solution to this problem just a pointer in the right direction because I amd totally lost on this.


java.util.concurrent.Semaphore


I had similar homework few years ago at my university, but in C++. Java is too high level language for this kind of stuff.

Here is my implementation of signal and wait in C++, but I don't know if it is going to be helpful because you will have to implement a lot of other things.

int KernelSem::wait() {
    lock();
    if(--value < 0) {
        PCB::running->state = PCB::BLOCKED;
        PCB::running->waitingAtSem = this;
        blockedQueue->put(PCB::running);
        dispatch();
    }
    else {
        PCB::running->deblockedBy = 0;
        if(semPreempt) dispatch();
    }
    unlock();
    return PCB::running->deblockedBy;
}


void KernelSem::signal() {
    lock();
    if(value++ < 0) {
        PCB* tempPCB = blockedQueue->get();
        if(tempPCB) {
            tempPCB->state = PCB::READY;
            tempPCB->deblockedBy = 0;
            tempPCB->waitingAtSem = 0;
            Scheduler::put(tempPCB);
        }
    }
    if(semPreempt) dispatch();
    unlock();
}

lock and unlock functions are just asm{cli} and asm{sti} (clear/set interrupt flag). PCB is a process control block.

Hope it helps


in a very simple simple (again) simple way you could implement this using a simple int or boolean.

Test the int or boolean before grant acess. If it is 0 (tired of boolean), add 1 and continue. If not do Thread.yield() and try again latter. When you release, remove 1 from int and continue.

naive implementation, but works fine.


I hope that this is homework, because I cannot see any good reason you might want to do this in production code. Wikipedia has a list of algorithms for implementing semaphores in software.


Doing as proposed in the accepted answer will lead to a lot of concurrent issues as you can't ensure mutual exclusion with this. As an example, two threads asking to increment an integer would both read the boolean (that is proposed as lock) the same time, then both will think it's ok and then both set the bool to its opposite value. Both threads will go in changing stuff and when they are done they will both write a value to the (non)mutually exclusive variable and the whole purpose of the semaphore is lost. The wait() method is for waiting until something happen, and that's exactly what you want to do.

If you absolutely don't want to use wait, then implement some kind of double checking sleep technique where the thread first check the lock variable, changes it to false and sets a flag in an array or something with a special slot just for that thread to ensure that it will always succeed. Then the thread can sleep for a small interval of time and then checks the whole array for more flags to see if someone else were at it the same time. If not, it can continue, else it can't continue and have to sleep for a random amount of time before trying again (to make the threads sleep for lengths to make someone success later). If they collapse again then they will sleep for an even longer random time. This technique is also used in networks where semaphores cannot be used.

(Of course semaphores is exactly what you want to do but as it uses wait i kind of assumed you wanted something that don't use wait at all...)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号