确认程序流程

发布于 2024-08-23 07:57:56 字数 1648 浏览 9 评论 0原文

有人能告诉我下面的代码是否可以正常工作吗?

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();

}  

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();

}  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千纸鹤 2024-08-30 07:57:56

不,这段代码甚至无法编译。例如,您的“UseFrequency”类有一个构造函数和一个 run() 方法,但您有两行 CriticalSection.freq[iFrequency] = true;
stop(); 不在任何方法体中 - 它们只是独立存在。

如果你得到代码来编译它仍然不会像你期望的那样工作,因为你有多个线程并且没有并发控制。这意味着不同的线程可以“互相踩踏”并损坏共享数据,例如“freq”数组。每当您有多个线程时,您都需要使用同步块来保护对共享变量的访问。关于并发的 Java 教程在这里解释了这一点 http://java. sun.com/docs/books/tutorial/essential/concurrency/index.html

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

不甘平庸 2024-08-30 07:57:56

您是否尝试过编译和测试它?您是否使用 Eclipse 这样的 IDE?您可以在调试器中单步执行程序以查看其功能。您的问题的结构方式没有人可以告诉您的程序是否在做正确或错误的事情,因为代码注释中没有指定任何内容,也没有在提出的问题中指定任何内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文