11 Replies - 484 Views - Last Post: 29 February 2012 - 03:20 AM Rate Topic: -----

#1 zehawk  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 213
  • Joined: 23-February 12

Threads? Are they as bad as they seem?

Posted 27 February 2012 - 03:55 PM

Just a question, I'm making a 2d game, right? I use a timer to do everything, but was wondering if threads would be faster? Only thing is that threads seem much scarier than a timer, and the timer updates fast. However, I know I may have to upgrade to threads, but they seem really really complex. Are they as bad as i think they are? Or do you find them easy? Also, do you think that I could get by using a timer for updating? I honestly dint think this is realistic. All links to good threading tuts are accepted!

This post has been edited by zehawk: 27 February 2012 - 03:56 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Threads? Are they as bad as they seem?

#2 supercorey  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 119
  • View blog
  • Posts: 207
  • Joined: 15-February 09

Re: Threads? Are they as bad as they seem?

Posted 27 February 2012 - 04:25 PM

Okay, threads may seem rather complicated at first along with everything concurrent, but they really are quite useful and powerful tools. The official Oracle Concurrency Tutorials do a very good job of presenting this concept.

Secondly, if you are using Swing for your game, which would seem most likely, you wouldn't be able to use threads well. Swing isn't thread-safe which means that using threads can cause unexpected behavior. For now you can easily stick with a timer which should serve the purpose perfectly well.

Good luck and cheers!
Was This Post Helpful? 1
  • +
  • -

#3 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 247
  • Joined: 21-June 11

Re: Threads? Are they as bad as they seem?

Posted 27 February 2012 - 04:26 PM

Well, as far as I've seen people tend to use Timers for updating quite often. Even I do if I'm working on a game :P

But since there's another person on this post (for longer than it'd take to read your question) I'm willing to bet he's going to answer it more in depth.

EDIT: Dang it! Posted after corey got to you.

This post has been edited by Crockeo: 27 February 2012 - 04:27 PM

Was This Post Helpful? 1
  • +
  • -

#4 zehawk  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 213
  • Joined: 23-February 12

Re: Threads? Are they as bad as they seem?

Posted 27 February 2012 - 04:31 PM

Hmm, do you think oracle will update that so swing is thread safe? I ask this cause in going to make this a really big game, and most games dont use timers to update everything at once. Im only 15 so I have quiet a lot to learn still lol. Hmm, ill check out the tut! But is there a way to still use threads with swing? Im using swing, yea. But I feel like one time constantly updating everything is ineffecient in the long run. Or do threads not even increase performance?
Was This Post Helpful? 0
  • +
  • -

#5 supercorey  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 119
  • View blog
  • Posts: 207
  • Joined: 15-February 09

Re: Threads? Are they as bad as they seem?

Posted 27 February 2012 - 04:35 PM

There are actually ways to use Swing in a thread-safe way, but they aren't immediately obvious. If you'd like to read more on the topic, there is an article here.

Cheers!
Was This Post Helpful? 2
  • +
  • -

#6 zehawk  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 213
  • Joined: 23-February 12

Re: Threads? Are they as bad as they seem?

Posted 27 February 2012 - 04:38 PM

Hey man, yuve proven to be a big help!! Ill give u a plus for that!
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Threads? Are they as bad as they seem?

Posted 27 February 2012 - 10:03 PM

View Postzehawk, on 27 February 2012 - 06:31 PM, said:

Hmm, do you think oracle will update that so swing is thread safe?

Don't see they would an effort for that. There is no valid reasons. Components that need to be thread safe, like JTextArea, already are.

Quote

I ask this cause in going to make this a really big game, and most games dont use timers to update everything at once.

So they are badly designed :)
Stay with Java Timer it is lot safer unless you really know what you are doing.
You can use Thread to read file, an Image or update a database but using Thread to update GUI components is not a good idea and no reason to complicate your life so make sure your GUI is update synchronously.
Was This Post Helpful? 0
  • +
  • -

#8 zehawk  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 213
  • Joined: 23-February 12

Re: Threads? Are they as bad as they seem?

Posted 28 February 2012 - 08:34 AM

How about having a thread to update everything like positions and lighting, instead of the actual gui components?
Was This Post Helpful? 0
  • +
  • -

#9 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 519
  • Joined: 14-January 09

Re: Threads? Are they as bad as they seem?

Posted 28 February 2012 - 08:40 AM

"I ask this cause in going to make this a really big game, and most games dont use timers to update everything at once."

Usually 'big' games don't use java, or at the very least don't use swing; if you really want to be updating panes with stuff on them you should just use something awt related and then put all your drawing into paint(Graphics g) so that you can do all the paint work there as things go, and move stuff around from another thread/etc
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Threads? Are they as bad as they seem?

Posted 28 February 2012 - 08:40 AM

You should and can do that with Timer. Timer calls actionPerformed() repeatedly. What you can do there is update the internal state of all your variables, such as changing lighting variables and positions. Then you can have each component update itself based on the changes. Even big games - professional games- do that. They have a single, overarching loop and then time share each component.

You can use threads for things like Saving a game WHILE playing (in the main loop). That's the way it works.

I have a very basic tutorial here:
http://www.dreaminco...e-rate-in-java/
Was This Post Helpful? 3
  • +
  • -

#11 zehawk  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 213
  • Joined: 23-February 12

Re: Threads? Are they as bad as they seem?

Posted 28 February 2012 - 09:02 AM

Ok thanks! Now all my questions are answered ! ill do that then, I had no idea games did that! I'm new to game dev, as you can tell... Anyways, thanks! This all helped!
Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Threads? Are they as bad as they seem?

Posted 29 February 2012 - 03:20 AM

One more point that may help. The reason you may have thought Threads were used for that is because frequently they are used to perform very complicated calculations, and to update the screen periodically during the calculation.

For this particular task, you will want SwingWorker,which will mimic multithreading in Swing and allows for periodic updates. However, it takes place in Swing's main loop so that it is synchronized.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1