Java :/

Associate
Joined
26 Jan 2006
Posts
1,502
Hello guys,

I am working with dynamic objects in my program.

Very simple,

A Vector is hosting all objects created and with a for loop I go through all of them one at a time calling their methods and changing their behavior.

So far so good.

However, After I have like 100 Objects in there, the loop is big and the single thread gets slow (the animations are on/off the objects perform on screen) so I thought "There must be a way to spawn another thread? or one thread for every 5 of them?".

And if I can do that and create threads, then I can put a thread inside the super class so all subclass (objects) inherit the code from it and perform everything on their own thread?

Lots of questions, but the java tutorial doesnt cover this :)

THANKS for any guidance.
 
http://java.sun.com/docs/books/tutorial/essential/concurrency/

I don't think multithreading in this case will speed it up really.

What you could do mind is have one thread doing the drawing and one thread doing the iteration. You would have to ensure proper synchronization mind.

I got a feeling Vectors are quite slow since they are thread safe by default you may want to try using a LinkedList instead if you don't need concurrent access. (I may be wrong about this)
 
Last edited:
As said above, it's hard to say if multithreading will help.

One thing I would say though is that usually you want to make GUIs event based rather than loop based, that is to say that you only do something on particular events.

Using an array rather than a vector may speed up some things (assuming you know the length of array needed at compile time), but without knowing more details it's hard to say.

Multithreading in Java is quite easy to do, the problem comes is ensuring that you don't start suffering from classic multithreading problems.
 
Back
Top Bottom