在操作栏事件的android杀特定线程 [英] android kill specific thread on action bar event

查看:160
本文介绍了在操作栏事件的android杀特定线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要杀死一个特定的线程,但无法弄清楚使它工作的一种方式。 我包括所有的信息旁边,即使它似乎并不重要作用:

I want to kill an specific thread but can't figure out a way of make it work. I include all info next, even if it does not seem important:

我用行动吧福尔摩斯,想杀死一个动作按钮事件线程。所以,我有:

I use action bar sherlock and want to kill a thread on an action button event. So, i have:

    Thread myThread;
    myThread = new Thread(new Runnable(){
        public void run(){
                functionX();
        }
      });
    myThread.start();

这线程是一个长期运行的线程,funcionX()创建一些新的线程为好。 我要杀死当线程:

This thread is a long running thread, and funcionX() creates some new threads as well. I want to kill the thread when:

   public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()) {

    case android.R.id.home:
        myThread.interrupt();
        break;
    };

我试着ExecutorService的使用提出(可运行),为未来的结果,Future.cancel,但似乎并不工作。 我还要提到的是functionX()使用HTTP GET请求从JSON服务中获取数据。

I've tried with ExecutorService, using submit(runnable) for a Future result and Future.cancel but does not seems to work. I should also mention that functionX() uses http get requests to get data from a JSON service.

推荐答案

有多种方法可以做到这一点,他们夫妇可以像

there are multiple ways you can do this,couple of them can be like

  1. 而不是线程使用的TimerTask和调用timeTask.cancel() - 用于中断
  2. 在该线程的run()方法,保持检查一个布尔值,以确定线程是否应该被终止,那么你就可以调用中断或更好抛出一个异常

这篇关于在操作栏事件的android杀特定线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆