Java缓冲区策略导致严重滞后
我有一个小引擎,在我的OSX笔记本电脑上运行得很好,但在功能不太强大的Linux PC上运行时会崩溃或严重滞后。我将代码最小化,使其仅为一个小类,但存在完全相同的滞后性。我认为这与缓冲策略和线程有关。下面是课程:
public class Test extends Canvas implements Runnable {
private Thread thread;
private boolean running = false;
public Test()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(1000, 1000));
frame.add(this);
frame.setVisible(true);
start();
}
public static void main(String[] args)
{
new Test();
}
public synchronized void start()
{
this.thread = new Thread(this);
this.thread.start();
this.running = true;
}
public synchronized void stop()
{
try
{
this.thread.join();
this.running = false;
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void run()
{
while(running)
{
render();
}
}
private void render()
{
BufferStrategy bs = this.getBufferStrategy();
if(bs == null)
{
this.createBufferStrategy(2);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, 1000, 1000);
g.dispose();
bs.show();
}
}
共 (0) 个答案