Good practice for multithreading

I have an application where, when the "game statistics", it launches a couple of different threads. I start topics like this:

Thread thread = new Thread(new Runnable() { public void run() { //... } }); thread.setName("killMeAtEnd"); thread.start(); 

Later, when the game ends, I have a dispose () method inside the game that sorts all current threads and ends all threads that have the name "killMeAtEnd". My question is, is this a good practice? My intention is for my application to work quickly and randomly, in my experience. Topics that remain “hanging” tend to slow down the phone until the application is terminated. Is there a better way to do this? Is it even worth the worry?

EDIT:

Here is my dispose() if anyone is interested. This code is in the Game class.

 public void dispose() { Thread threads[] = (Thread[])Thread.getAllStackTraces().keySet().toArray(); for(int x=0;x<threads.length;x++) { Thread thread = threads[x]; if(thread.getName().equalsIgnoreCase(KILL)) { try { thread.interrupt(); }catch(Exception e){Log.e(Viewer.GAME,Log.getStackTraceString(e));} thread = null; } } } public static final String KILL = "endOnDispose"; 
+7
source share
3 answers

You have the right idea, but there are some improvements:

  • Instead of querying the system for all running threads, just add your threads to the list whenever you create them. Then you can complete all the threads you created or wait for them to complete (join).
  • An interrupt only interrupts a thread in a locked state, so you need to have an additional flag that checks the thread periodically (that is, after each "work cycle").
  • Catch the interrupt exception inside your thread and handle it (i.e. exit gracefully).
+4
source

This is not necessarily a bad solution, but there are several problems:

  • If your threads are sort workflows that process one task to completion, there is probably a better design in which the thread ends by itself. In other words, perhaps the best stream of execution that does not require to be killed at the end.

  • When you say "sort all current topics ...", I suppose you look at ALL running threads in the JVM? like something like this SO question ? If so, why don't you just reference all the threads that your game owns and then specifically kill them? Unlike just searching for "killMeAtEnd"; I can’t figure out how your strategy might go wrong, but it’s a little better to keep track of your flows.

It is definitely a good practice to keep threads clean. If your threads are doing something less task-specific (like waiting for an io network or something else), my first sentence is somewhat irrelevant. I just suggest being very careful about how you keep your threads clean, because errors with threads can be a big pain.

+3
source

The ExecutorService class already exists to solve this problem.

Set all your threads for the ExecutorService.

When the game ends, turn off the ExecutorService using shutdownNow. This will abort all threads in the ExecutorService. You can then create a new ExecutorService when starting a new game.

If the number of threads is fixed, you can use Executors.newFixedThreadPool () to create an ExecutorService.

If the number of threads is a variable, you can use Executors.newCachedThreadPool to create an ExecutorService.

0
source

All Articles