Initialising threads with a while loop.

Hello there :slight_smile:

I am making a simple particle system, to practice. I want to have a loop, that initialises a threads, and then waits for 1 second.
Now, I am good with the timer (i think…) but where my problem appears, is where I initialise the threads. Because normally I would do it like this:


Thread particle = new Thread(new ParticleAI));
particle.start; //I use two lines, instead of one because I am a bit of a retard  :P

So basicly: How do I initialise a thread, without having to name it (so I can initialise more threads, from the same while loop)??

Thanks in advance

Hmm if you won’t name the Thread you can have memory leaks in your soft, cuz some of the threads could not finish for some reason… Also if you gonna create the threads and don’t save reference to it, you are loosing control over the threads you created.

I’m not sure of the best solution for this, but I would use something like:


Vector <Thread> particle_thread_vector = new Vector <Thread>();
...
while(true)//put your condition here
{
...
Thread tmp_thread = new Thread(new ParticleAI());
tmp_thread.start();
particle_thread_vector.addElement(tmp_thread);
...
}

Yes, but in the next loop you would initialise a thread with the same name - doesn’t that cause problems?
And that vector - doesn’t it slow the machine down if the computer is running for long enough because of the vector size getting big?
Or should I empty the vector, after my particles are gone?

Initializing the Thread with same name in this case won’t cause problems because of using new Thread(), which will create new instance of the object(in this case Thread), overwriting the old one.

What about Vector, I think(almost 100% sure) that after the death of the thread you will need to remove it from Vector, else Vector will keep growing in it’s size.

Other than non-determnism, unpredictable update periods, degraded performance, additional debugging problems and a whole extra class of syncronisation and threading bugs, what exactly are you hoping to gain by putting individual particle systems in their own threads?

To be able to give each particle a simple AI? How should it be done? :slight_smile:

Just a regular game loop, the way you give any kind of game object some simple behaviour. You just need some kind of top level loop that every frame updates all of your objects and then draws them to the screen.

Orangy_tang is absolutly right,

1000 particles => 1000 Threads ? that what you intended to do ? it is really not a good way

you could do something like that :


public class Particle
{
	private double someProperties;
	private double someOtherProperties;
	
	public void update()
	{
		this.someProperties+=this.someOtherProperties;
	}
	...
	some other methods
	....
}

than later in main thread (or a child thread)


Particle p[]=new Particle[10000];
for(int n=0;n<p.length;n++)
	p[n]=new Particle();
while(this.mustRun)
{
	p.update();
	p.draw();
}