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 的线程
基础教程推荐
- 将 Windows 证书导入 Java 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- 将 double 转换为 Int,向下舍入 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
