简体   繁体   中英

How many Threads in Java 2D game?

Im quite new here, and Im learning java for 2 months. 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). 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).

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). 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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