Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

Java, is starting and stopping a thread like this ok?

This topic is 4074 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

In my program I've got an animated map(has a while loop) that runs on a separate thread, and to stop it, i set a flag in animated map so it will exit its while loop. Then to start the thread again, I create a new thread and start it. Could there be any problems with doing it this way? Also how do I pause a thread so I can update some shared variables and then resume again? (aka being thread safe) thx

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by johnnyBravo
In my program I've got an animated map(has a while loop) that runs on a separate thread, and to stop it, i set a flag in animated map so it will exit its while loop.


That's fine. Generally, you'll want to declare the flag as volatile:


public class MyClass implements Runnable
{
private Thread thread = null;
private volatile boolean running = false;

public void start()
{
if(thread != null) return;

thread = new Thread(this);
thread.start();
}

public void run()
{
running = true;
while(running)
{
...
}
}

public void stop()
{
running = false;
thread = null;
}
}



Using this approach, if the object can be manipulated from multiple threads, you'll want to synchronize the start and stop methods to make manipulation of the thread object thread-safe.

Quote:
Then to start the thread again, I create a new thread and start it.


That's fine, but keep in mind that creating threads has a certain amount of overhead. If you do it frequently, you can hurt performance. In that case, you want might want to consider a thread pool.

A better option would be to use an instance of java.util.concurrent.ExecutorService. This will manage a thread pool for you. You can configure one to run the same task repeatedly at different intervals, or as frequently as you feed tasks to it.

Quote:

Also how do I pause a thread so I can update some shared variables and then resume again? (aka being thread safe)


That's not being thread-safe, it's being inefficient. To safely update shared data, use synchronization.

Share this post


Link to post
Share on other sites
Thank you,


From what I've read so far, I think to synchronize I just need to add the 'synchronized' keyword in front of the functions?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!