Sign in to follow this  

[java] A little bit of theory on threading!

This topic is 2849 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How should I thread a swing based RTS? I was previously using ignoreRepaint(true) for all my components and repainting manually all in one thread to avoid concurrent mod exceptions. What is the correct way to do this? For smoothness, efficiency, etc. I imagine it would be a nightmare to program every in game entity to know when it needs repainting, so updating the gui at fixed intervals still seems desirable. Thoughts!

Share this post


Link to post
Share on other sites
That will only work if your game environment updates at the same rate as you update the screen. That's fine for a fighting game, but 30-60 physics updates per second sounds excessive for an RTS.

I suggest using a swing Timer to run each type of task from the event dispatch thread. That way you'll be accessing and modifying data from only one thread and you will have no concurrency issues.

In code, this would be:

Timer physics = new Timer(100, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// updates physics 10x per second
}
});
Timer animation = new Timer(30, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// updates animation ~30x per second
repaint(); // repaint normally; no need for immediate drawing
}
});
physics.start();
animation.start();

Share this post


Link to post
Share on other sites
Quote:
Original post by Dathgale
That will only work if your game environment updates at the same rate as you update the screen. That's fine for a fighting game, but 30-60 physics updates per second sounds excessive for an RTS.

I suggest using a swing Timer to run each type of task from the event dispatch thread. That way you'll be accessing and modifying data from only one thread and you will have no concurrency issues.

In code, this would be:
*** Source Snippet Removed ***


Would this fix concurrent modification exceptions? What about the whole invokeLater() usage?

Here is some in-game footage of what i have now. The scrolling glitch with fields of stray lines only appears during video recording.

http://www.youtube.com/watch?v=GICc2oJFwVA

Share this post


Link to post
Share on other sites
Quote:
Original post by overeasy
Would this fix concurrent modification exceptions?

Concurrent modification exceptions occur if you modify a collection while iterating over it. This can be caused by multithreading, but a thread can cause that exception by itself like so:

List list = ...;
for(Object element: list)
list.add(...); // <-- throws ConcurrentModificationException


If you don't do anything like the above, then single-threading your application will solve the concurrent modification problems.

Quote:
What about the whole invokeLater() usage?


EventQueue.invokeLater(new Runnable() {
public void run() {
// This runs in the event dispatch thread at the next opportunity.
}
});

There's not much to this method. Read the documentation if you still don't understand.

Share this post


Link to post
Share on other sites

This topic is 2849 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this