事件调度线程绘画

发布于 2024-12-22 20:54:43 字数 626 浏览 2 评论 0原文

简单说一下 - 我正在编写一个 Sega Master System 模拟器。到目前为止的设计是 GUI(JFrame 子类等)通过 EventQueue.invokeLater() 调用在事件调度线程中运行,并且所有模拟器功能在单独的线程中运行。据我了解,这应该是一个很好的实践,因为 EDT 应该初始化和更新 GUI,而 Swing 不是线程安全的。

这让我想到一个小问题 - 控制台在单独的线程(Z80、VDP 等)中运行,并且 VDP(当我完成它时)将更新 BufferedImage。只要我将此 BufferedImage 标记为“同步”,从 EDT(特别是 GUI 的绘制方法)访问它是否安全?我问这个问题,否则我将不得不将大量 VDP 逻辑放入绘图方法本身中,而且我宁愿不这样做,因为它会整体减慢 GUI 的速度。

我知道这个简单的问题,但我仍然习惯于推动 Java2D 以获得不错的速度动画。值得一提的是,在逻辑线程中调用 repaint() 方法之后,逻辑线程将休眠,直到唤醒下一帧传递,因此我猜测此设计不会对性能造成影响 - am我说得对吗?

非常感谢, Phil Potter

更新 我应该使用更好的语言 - VDP 将通过同步 setter 方法访问 BufferedImage,而 EDT 将通过同步 getter 方法访问它。

Just a quick one - I'm in the process of writing a Sega Master System emulator. The design thus far is that the GUI (JFrame subclass etc.) runs in the Event Dispatch Thread, via an EventQueue.invokeLater() call, and all the emulator functions run in a separate thread. From what I understand this is supposed to be good practice, as the EDT is supposed to initialise and update the GUI, with Swing not being thread-safe.

This leads me to a small question - the console runs in the separate thread (Z80, VDP, etc.) and the VDP (when I've finished it) will update a BufferedImage. As long as I mark this BufferedImage as 'synchronized', is it safe to then access it from the EDT (the GUI's paint method in particular)? I ask this as otherwise I will have to put a lot of VDP logic into the paint method itself, and I'd rather not do this, as it will slow down the GUI overall.

Simple question I know, but I'm still getting used to pushing Java2D for decent speed animation. It is worth mentioning that right after the repaint() method is called in the logic thread, the logic thread will sleep until woken for the next frame pass, so I'm guessing there won't be a performance hit from this design - am I right?

Many thanks,
Phil Potter

UPDATE I should have used better language - the VDP will be accessing the BufferedImage through a synchronized setter method and the EDT will be accessing it through a synchronized getter method.

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

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

发布评论

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

评论(1

听闻余生 2024-12-29 20:54:43

我认为您不需要使 setter 和 getter 同步,因为它们只能从 EDT 访问。那么让我们退后一步。您说过将从 EDT 访问 getter ——所以我们在这里是线程安全的。现在,setter 方法将在 VDP 上运行——为了处理这种情况,我们要调用 invokeLater(请参阅示例)。请查看此处了解更多信息。

Runnable updateAComponent = new Runnable() {

    public void run() { 
//this will run in the EDT
component.doSomething(); 

}

};
//this will run in the VDP
SwingUtilities.invokeLater(updateAComponent);

I don’t think you need to make the setter and getter synchronized since they should only be accessed from EDT. So lets take a step back. You said that the getter will be accessed from EDT -- so we are thread safe here. Now with the setter method will be run on the VDP -- to handle this case we want to call invokeLater ( see example). Take look here for more info.

Runnable updateAComponent = new Runnable() {

    public void run() { 
//this will run in the EDT
component.doSomething(); 

}

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