Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 静态方法很慢_Java_Performance - Fatal编程技术网

Java 静态方法很慢

Java 静态方法很慢,java,performance,Java,Performance,我正在用java编写一个简单的游戏。 我做了一个30 FPS的碰撞测试,我必须得到窗口的大小。 因为我没有访问GUI实例的权限,所以我想我应该创建一个共享实例,因为这在我所在的Objective-C中是相当标准的 class GUI extends JFrame { private static GUI _sharedInstance; public static GUI sharedInstance() { if (_sharedInstance == nul

我正在用java编写一个简单的游戏。 我做了一个30 FPS的碰撞测试,我必须得到窗口的大小。 因为我没有访问GUI实例的权限,所以我想我应该创建一个共享实例,因为这在我所在的Objective-C中是相当标准的

class GUI extends JFrame {
    private static GUI _sharedInstance;

    public static GUI sharedInstance() {
        if (_sharedInstance == null) {
            _sharedInstance = new GUI();
        }
        return _sharedInstance;
    }
}
但出于某种原因,它真的很慢。 然后我用
publicstaticfinal
实例替换了共享实例的大小,它现在运行得很快,即使每秒60帧或更高

谁能解释一下为什么会这样

编辑

因此,我没有调用
GUI.sharedInstance().getWidth()
,而是调用
GUI.windowSize.width
。 我使用了
公共静态最终维度windowSize

编辑

这是碰撞检测。 因此,不要调用
intwidth=GUI.kWindowWidth,,
我正在调用
intwidth=GUI.sharedInstance().getWidth()之前

// Appears on other side
if (kAppearsOnOtherSide) {
    int width = GUI.kWindowWidth;
    int height = GUI.kWindowHeight;

    // Slow
    // int width = GUI.sharedInstance().getWidth();
    // int width = GUI.sharedInstance().getHeight();

    Point p = this.getSnakeHead().getLocation();
    int headX = p.x;
    int headY = p.y;

    if (headX >= width) {
        this.getSnakeHead().setLocation(new Point(headX - width, headY));
    } else if (headX < 0) {
        this.getSnakeHead().setLocation(new Point(headX + width, headY));
    } else if (headY >= height) {
        this.getSnakeHead().setLocation(new Point(headX, headY - height));
    } else if (headY < 0) {
        this.getSnakeHead().setLocation(new Point(headX, headY + height));
    }
}
//显示在另一侧
如果(另一侧){
int width=GUI.kWindowWidth;
int height=GUI.kwindoweight;
//慢
//int width=GUI.sharedInstance().getWidth();
//int width=GUI.sharedInstance().getHeight();
点p=this.getSnakeHead().getLocation();
int headX=p.x;
int-headY=p.y;
如果(头X>=宽度){
this.getSnakeHead().setLocation(新点(headX-width,headY));
}否则如果(人头X<0){
this.getSnakeHead().setLocation(新点(头X+宽度,头Y));
}否则,如果(头晕>=高度){
this.getSnakeHead().setLocation(新点(headX,headY-height));
}否则如果(令人兴奋<0){
this.getSnakeHead().setLocation(新点(headX,headY+高度));
}
}

我知道这可能不是问题的真正答案,但可能是比赛条件的问题。 我假设您正在尝试使用lazy/singleton模式,因为GUI类的构造非常昂贵,您只需要一个实例

现在,发生了什么?如果一个线程运行到
If
语句体中,它将创建一个新的
GUI
实例。就在下一次分配之前,调度程序会暂停它。 现在另一个线程弹出,看到
\u sharedInstance
仍然为空,并创建另一个
GUI
实例(依此类推…)

假设只有两个线程。 第一个线程一直运行到最后,第二个线程继续运行,现在您有了GUI类的两个实例。没有人说未分配的GUI实例被破坏或垃圾回收。 这可以解释与最终使用和将GUI实例直接分配给
\u sharedInstance
相比,性能损失了50%

例如,参见
双重检查锁定、内部类或枚举是您可以使用的方法。

我认为相关-请解释您的意思。您用什么替换了什么,您是如何度量性能差异的?如果您只是简单地用变量引用替换方法,那么--duh!!方法调用总是比变量引用慢得多。每秒调用多少次。它可能相当快,但如果它被敲打,它将影响事情。你有什么可以描述的吗?事实上可能是这样。我想了很多<代码>新建GUI()
启动主循环。在那里,它更新并检查碰撞。如果GUI对象的初始化在
update()
之前尚未完成,则调用
GUI.sharedInstance()
将创建一个新对象。我要做一些测试。谢谢你是对的,因为我在构造函数中初始化并启动了更新线程,更新被调用得太快了,以至于GUI对象还没有设置。因此,GUI对象被初始化了3次,这就是为什么我会有如此多的性能损失。谢谢!