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 的线程


- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01