简体   繁体   English

Java 2D游戏中有多少个线程?

[英]How many Threads in Java 2D game?

Im quite new here, and Im learning java for 2 months. 我在这里很新,我学习Java了2个月。 Im actually learning about Threads and multithreading, and I have a little question.To practice Im writing a simple 2D Cards Game (like MTG or HearthStone). 我实际上是在学习线程和多线程,我有一个小问题。要练习写一个简单的2D纸牌游戏(例如MTG或HearthStone)。 I've done a lot for this moment, but Im wondering how many Threads I have to use, to create the most efficient application and to acquire good habits. 我现在已经做了很多事情,但是我想知道我必须使用多少个线程,才能创建最高效的应用程序并养成良好的习惯。

So, for now I have JFrame in EventQueue, and other class extending JPanel and implementing Runnable, which is my Board and which has game loop (with init(), uptade(), and repaint() and Mouse Listeners methods). 因此,目前,我在EventQueue中具有JFrame,并且其他类扩展了JPanel并实现Runnable,这是我的Board,并且具有游戏循环(具有init(),uptade()和repaint()以及Mouse Listeners方法)。

It this good for the simple game? 这对简单游戏有好处吗? Or perhaps every Card on the Board should have a separate thread for displaying Card information, repainting etc. ? 还是板上的每个卡都应该有一个单独的线程来显示卡信息,重新粉刷等?

I am grateful for every help, Cheers! 感谢您的帮助,干杯!

Threading will greatly increase the complexity of your program, so use it very sparingly. 线程化将大大增加程序的复杂性,因此请谨慎使用。 (It's also a big commitment of system resources, but that's unlikely to be your big problem here) (这也是系统资源的重大承诺,但这在这里不太可能成为您的大问题)

I'd suggest to start with, make your application single threaded. 我建议从头开始,使您的应用程序为单线程。 If you find the game logic takes so long that it makes the UI sluggish and unresponsive, you might look into running the UI and the game logic on separate threads, but this is quite unlikely (unless your card game is seriously complicated). 如果您发现游戏逻辑花费了很长时间以至于使UI变得缓慢且无响应,则可以考虑在单独的线程上运行UI和游戏逻辑,但这是不太可能的(除非您的纸牌游戏非常复杂)。 Really the place to start thinking about multi-threaded models would be when you have several clients connecting - then each client would need their own thread, and you'd have to deal with the interactions between them. 真正开始考虑多线程模型的地方是当您有多个客户端连接时-每个客户端都需要自己的线程,并且您必须处理它们之间的交互。

The best advice I've seen on threads in Java is that you'll almost never need to handle them explicitly. 我在Java线程中看到的最好建议是,几乎不需要显式处理它们。 I'd suggest if you want to put some multi-threading into your application, do it as a training exercise, but be aware this probably isn't something you'd do in production. 我建议您是否要在应用程序中添加一些多线程,将其作为培训练习来进行,但请注意,这可能不是您在生产中要做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM