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


基础教程推荐
- 验证是否调用了所有 getter 方法 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 大摇大摆的枚举 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java Swing计时器未清除 2022-01-01