views:

75

answers:

2

can someone tell if the code below would work fine?

class CriticalSection{

int iProcessId, iCounter=0;

public static boolean[] freq = new boolean[Global.iParameter[2]];

int busy;

//constructors

CriticalSection(){}

CriticalSection(int iPid){
    this.iProcessId = iPid;
}

int freqAvailable(){

for(int i=0; i<
Global.iParameter[2]; i++){

        if(freq[i]==true){
            //this means that there is no frequency available and the request will be dropped
            iCounter++;
        }   
    }
    if(iCounter == freq.length)
        return 3;

    BaseStaInstance.iNumReq++;
    return enterCritical();
}

int enterCritical(){

    int busy=0;
    for(int i=0; i<Global.iParameter[2]; i++){
        if(freq[i]==true){
            freq[i] = false;
            break;
        }
    }
    //implement a thread that will execute the critical section simultaneously as the (contd down)
    //basestation leaves it critical section and then generates another request
    UseFrequency freqInUse = new UseFrequency;
    busy = freqInUse.start(i);

//returns control back to the main program

    return 1;   
}
}

class UseFrequency extends Thread {

    int iFrequency=0;

     UseFrequency(int i){
        this.iFrequency = i;
     }
     //this class just allows the frequency to be used in parallel as the other basestations carry on making requests
    public void run() {
        try {
            sleep(((int) (Math.random() * (Global.iParameter[5] - Global.iParameter[4] + 1) ) + Global.iParameter[4])*1000);
        } catch (InterruptedException e) { }
    }

    CriticalSection.freq[iFrequency] = true;

    stop();

}  
A: 

Have you tried compiling and testing it? Are you using an IDE like Eclipse? You can step through your program in the debugger to see what its doing. The way your question is structured no one can tell either way if your program is doing the right or wrong thing, because nothing is specified in the comments of the code, nor in the question posed.

Amir Afghani
+2  A: 

No, this code will not even compile. For example, your "UseFrequency" class has a constructor and a run() method, but then you have two lines CriticalSection.freq[iFrequency] = true; and stop(); that aren't in any method body - they are just sitting there on their own.

If you get the code to compile it still will not work like you expect because you have multiple threads and no concurrency control. That means the different threads can "step on eachother" and corrupt shared data, like your "freq" array. Any time you have multiple threads you need to protect access to shared variables with a synchronized block. The Java Tutorial on concurrency explains this here http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html

Jemiah
CriticalSection.freq[iFrequency] = true just sets the boolean array from the CriticalSection class back to true.and Ive used stop() to kill the current thread.But yes, I understand what you've mentioned about the synchronized block. Would the code below ensure concurrency? Ive omitted most of the code, synchronized block concerns me.int enterCritical(){ int busy=0; for(int i=0; i<Global.iParameter[2]; i++){ synchronized(Global.freqLock){ if(freq[i]==true){ freq[i] = false; break; } } } synchronized(Global.freqLock){ CriticalSection.freq[iFrequency] = true; }
rookie
@user282544: Comments don't have formatting, you should really edit your original post when you have changes like this. I will point out, however, that stop() is deprecated, and with good reason: http://java.sun.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html
Lord Torgamus
thank you very much for your reply. Thanks also for bringing to my notice the idea of editing my original post with code changes, it did not occur to me
rookie