有人可以告訴下面的代碼是否可以正常工作?確認程序流程
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();
}
也許如果你正確地重新格式化/重新加載代碼。縮進是虛假的,這使得代碼很難解釋。如果您使用的是Eclipse,只需按下「Ctrl + Shift + F」即可。進一步詳細講述代碼的實際問題。怎麼了?不會發生什麼?它不夠嗎?我們可以複製'n'paste'n'run的SSCCE(http://sscce.org)會很棒。 – BalusC 2010-03-02 18:49:08
而不是張貼一大塊代碼,你可能會考慮發佈有關代碼的更具體的問題與相關的片段。或者至少格式正確;沒有縮進就很難閱讀。 – 2010-03-02 18:49:32
如果你不告訴我它打算做什麼,我該如何告訴你它是否會起作用?目前爲 – Pops 2010-03-02 18:49:57