wait()方法的线程不工作的Andr​​oid [英] wait() method on thread not working Android

查看:111
本文介绍了wait()方法的线程不工作的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有形式提交任务到的ThreadPoolExecutor 几个下载。现在,我在扩展应用程序的全局类创建这个的ThreadPoolExecutor 。我存储所有在的HashMap 提交的任务,IDS。

I have a few downloads that are submitted as tasks to a ThreadPoolExecutor. Now, I am creating this ThreadPoolExecutor in a global class that extends Application. I am storing all the submitted tasks in a HashMap with ids.

我有一个的ListView 片段。这的ListView 项目包含暂停和恢复按钮。当我点击列表中的项目本身,在下载 FutureTask提供提交到全局池执行人。现在,当我点击的ListView 项目的暂停按钮,我想要那个特定的线程暂停/等待。

I have a ListView in a Fragment. This ListView item contains pause and resume buttons. When I click on the list item itself, the download FutureTask is submitted to the global pool executor. Now, when I click on the pause button of the ListView item, I want that particular thread to pause/wait.

我暂停键在我的列表视图的定制适配器的onClick 方法。所以,当我按一下按钮,在我的适配器类,我得到当前正在运行,把它们放在一个数组,然后用得到我从阵列所需的名称线程的所有线程。我可以在日志中看到该线程我想与我设置的名称运行。所以一旦我得到的线索,我做的等待()就可以了。

I have the onClick method of the pause button in my list view's custom adapter. So, when I click the button, in my adapter class, I get all the threads that are currently running, put them in an array and then get the thread with the name I want from the array. I can see in my log that the thread I want to get is running with the name I set. So once I get that thread, I do wait() on it.

的onclick 我的恢复按钮,我通知特定线程,并设置标志暂停假,这样线程恢复。但是,这里面线程下载实际上,即使保持暂停点击运行后。我不知道我要去的地方错了。

In the onclick of my resume button, I notify that particular thread and set the flag to pause as false so that the thread resumes. But, the download inside that thread actually keeps running even after clicking pause. I do not know where I am going wrong.

我GlobalState类:

My GlobalState Class:

public class GlobalState extends Application{

HashMap<String, Future<?>> futureMapMain = new HashMap<String, Future<?>>();

    ThreadPoolExecutor mainExec = new ThreadPoolExecutor(2, 2, 2000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new YourThreadFactory());

public void submitTaskMain(String name, Thread task){
        Future<?> longRunningTaskFuture = mainExec.submit(task);
        futureMapMain.put(name, longRunningTaskFuture);
    }

    public HashMap<String, Future<?>> getFutureMap(){
        return futureMapMain;
    }

    public ThreadPoolExecutor getMainExeutor(){
        return mainExec;
    }

public class YourThreadFactory implements ThreadFactory {
           public Thread newThread(Runnable r) {
             return new Thread(r, gettName());
           }
         }
}

这是我的片段类里面写我的下载方法。在列表项的点击得到执行:

My Download Method that is written inside my fragment class. gets executed on list item click:

public void abDownloadTask(){

Thread dThread = new Thread(new Runnable() {

                final Handler pdfHandler = new Handler();

                @Override
                public void run() {
                    for(something){
/* DOES SOME DOWNLOAD USING url.getcontent() with different urls in a loop and stores files to sd card. */
}
}
}
dThread.setName(threadname);
            Log.d("Tel Frag", "Thread name set as: "+dThread.getName());
            mainGs.settName(threadname);
            mainGs.submitTaskMain(threadname, dThread);
}

里面的自定义列表适配器我的暂停按钮的onclick:

onclick of my pause button inside custom list adapter:

    pause.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

                needToPause = true;

                Runnable runnable = new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub

                        Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
                        Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
                        Log.d("CLA", "Threads running size: "+threadArray.length); 

                        Thread neededThread = null;

                         for ( Thread thread : threadArray ){
                             Log.d("CustomListAdapter", "Thread name in array: "+thread.getName());
                                if (thread.getName( ).equals(threadname)){
                                    neededThread = thread;
                                }
                         }   

                         if(neededThread!=null){
                            while(needToPause){
                                synchronized  (neededThread) {
                                        try {
                                            neededThread.wait();
                                        } catch (InterruptedException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }
                                }
                            }
                         } 
                    }
                };
                new Thread(runnable).start();

                notifyDataSetChanged();
        }
    });

我的自定义列表适配器类里面写简历按钮的onclick:

onclick of my resume button written inside custom list adapter class:

resume.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            needToPause = false;

            Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
            Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);

             for ( Thread thread : threadArray ){
                    if ( thread.getName( ).equals(threadname) ){
                        thread.notify();
                    }
             }  


            notifyDataSetChanged();
        }
    });

我一直在试图弄明白了当前第2天,但没有运气。我经历了很多的例子和计算器的问题去了。我只是想通过名称来获得一个线程,然后暂停和恢复它。任何帮助将是AP preciated。谢谢!

I have been trying to figure it out for 2 days now but no luck. I went through a lot of examples and stackoverflow questions. I am just trying to get a thread by its name and then pause and resume it. Any help would be appreciated. Thank you!

推荐答案

您不能可靠地使用等待()通知()的对象。这是在Javadoc明确规定。你已经有了未来的对象,你不应该使用反正其它机构。

You can't reliably use wait() and notify() on Thread objects. This is clearly stated in the Javadoc. You already have Future objects, you shouldn't be using other mechanisms anyway.

这篇关于wait()方法的线程不工作的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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