0

In my Activity I create a Thread for multiply objects and "save" them to a hashmap. Is it possible to pause just a single thread of the hasmap and continue it?

public void onCreate(Bundle savedInstanceState) {    
  Thread oThread = new Thread(new Runnable() {
        public void run() {

          while (true) {
            // doing some work
          }
        }
  }
  oThread.start();
  aThreads.put(name, oThread);

}

public void anyMethod(){
  // here I want to start and continue a thread
  aThreads.get(name).stop();
  aThreads.get(name).resume();
}

But .stop() and .resume() aren't working :/ any suggestions?

Bene
  • 1,251
  • 6
  • 19
  • 34
  • 1
    Check this answer maybe this is what you trying to achieve : http://stackoverflow.com/a/6776463/665823 – Kalem Jul 30 '15 at 12:17
  • 1
    Check this answer http://stackoverflow.com/questions/6776327/how-to-pause-resume-thread-in-android/6776463#6776463 – Pollizzio Jul 30 '15 at 12:24

1 Answers1

0

Use a boolean variable to control the thread exection.

ie.

 while(!isThreadRunning){

...your statements
}

Once you are moving out of the screen in OnPause method make the variable false/true based on your usage to stop the thread execution.And you have to take care of the persisting hashmap.

Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42
Shriram
  • 4,343
  • 8
  • 37
  • 64