If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Thread Help

Ok here is a pretty strait forward question asked in a complicated way, I have an application that will create a new thread every time the mouse is clicked, obviously the threads created need to act independantly of each other, but for some reason when you click more than once, the faster the threads go.

The application i am making is just a simple little game, that is eventually going to be a scrolling shooter game, when the user clicks, a new object called laser is created, and then a thread for that laser is created which will move the laser up the screen by sleeping for 10milli seconds, then changing the y value of the laser, then calling repaint. When you click more than once, the lasers speed up and i think its because there are multiple threads with the same name and java is getting confused with the timings, here is the code for the main class in the game:

ok, to extend on this further, i guess another question i could ask for more of an overview of this problem, would be. How can i make it so that each time the user clicks the mouse, a new thread with a different name is created?

The naming of the variables have nothing to do with your problem. The names are just pointers, like a bit of string linked to the object. The problem is nothing to do with the names of the threads, but more to do with what you're putting in them.

What you have is one uber object that is listening to all the mouse events, and also running all the laser threads. When the user presses a mouse button, this object creates a new laser object and stores it in a vector. Then a new thread is created, and this uber object is added to it. The run method runs, and ALL the laser objects in the vector are updated.

The problem arrises when the mouse is clicked multiple times. The thread is created with the SAME uber object added to it. Imagine you have 10 threads created. They all have exactly the same uber object in it, which means each thread has access to exactly the same laser objects. There is no problem with the timing, each thread 'fires' every 10 milliseconds. But if you have 10 threads all updating the same lasers, every 10 milliseconds each laser is being updated 10 times!!! That is why they appear to have speeded up.

Instead of adding the entire JPanel to a new thread, instead you should add each individual laser to a new thread and it should be updating itself every 10 seconds. The JPanel should just repaint itself ever XX seconds, and shouldn't be bothered about updating any lasers.

and the this is the main problem, the thing is you must pass a thread an object with implements runnable, and if i pass it a laser object then ill have to make the laser class implement the runnable interface.
Im just a little confused as to what class should be doing the painting and what to do in the laser's run method etc...

The reason the threads are a problem is because you pass the same object to it each time, which means there are multiples of all the laser objects. This means they get updated more than once (once by each thread), making them appear to go faster.

You either need to make each laser a Runnable object and let them update themselves, or you need to have once thread that does all the updating. Either way, that would make sure that each laser only got updated once. Then you have the JPanel thread redraw itself once every x seconds, based on the positions of the lasers.