Android - 暂停并继续线程

时间:2015-07-30 12:13:23

标签: java android

在我的Activity中,我为多个对象创建一个Thread,并将它们“保存”到一个hashmap中。是否可以只暂停一个hasmap的一个线程并继续它?

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();
}

但.stop()和.resume()不起作用:/任何建议?

1 个答案:

答案 0 :(得分:0)

使用布尔变量来控制线程执行。

 while(!isThreadRunning){

...your statements
}

在OnPause方法中移出屏幕后,根据您的用法使变量为false / true,以停止执行线程。您必须处理持久化的hashmap。