Name the Thread of an AsyncTask(命名 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 的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:命名 AsyncTask 的线程


基础教程推荐
- 如何对 Java Hashmap 中的值求和 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01