命名 AsyncTask 的线程 [英] Name the Thread of an AsyncTask

查看:26
本文介绍了命名 AsyncTask 的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为 AsyncTask 的后台线程命名,就像 Java 中的普通线程一样:

Is it possible to give a name to an AsyncTask's background thread, as for normal Threads in Java:

Thread(Runnable target, String name) 

我看过AsyncTask的代码,默认构造函数就是默认给个名字

I have seen the code of AsyncTask and the default constructor is just give a name by default

private static final ThreadFactory sThreadFactory = new ThreadFactory() {
    private final AtomicInteger mCount = new AtomicInteger(1);

    public Thread newThread(Runnable r) {
        return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());
    }
};

我想这样做,因为在调试时我想知道哪个 Asynctask 正在访问辅助类中的方法.

I want to do that, as when I debug I want to know which Asynctask is accessing a method in a helper class.

推荐答案

尝试从 doInBackground() 内部调用它:

Try calling this from inside doInBackground():

Thread.currentThread().setName("some custom name");

这篇关于命名 AsyncTask 的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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