String msgToPrint = Thread.currentThread().getStackTrace()[3] .getMethodName();

就是调用时的方法名。

其中使用的Thread类的第一个方法:

public static Thread currentThread()

返回当前线程对象。

实例代码:

package com.loaderman.getthreadstatusdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); printStackInfos();
}
private void printStackInfos() {
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); System.out.println("Called in printStackInfos() method!!!!!!!");
System.out.println("The stackTraceElements length: " + stackTraceElements.length); for (int i = 0; i < stackTraceElements.length; ++i) {
System.out.println("---- the " + i + " element ----");
System.out.println("toString: " + stackTraceElements[i].toString());
System.out.println("ClassName: " + stackTraceElements[i].getClassName());
System.out.println("FileName: " + stackTraceElements[i].getFileName());
System.out.println("LineNumber: " + stackTraceElements[i].getLineNumber());
System.out.println("MethodName: " + stackTraceElements[i].getMethodName());
}
}
}

日志输出

03-17 04:48:10.088 701-701/com.loaderman.getthreadstatusdemo I/System.out: Called in printStackInfos() method!!!!!!!
03-17 04:48:10.098 701-701/com.loaderman.getthreadstatusdemo I/System.out: The stackTraceElements length: 18
03-17 04:48:10.108 701-701/com.loaderman.getthreadstatusdemo I/System.out: ----  the 0 element  ----
03-17 04:48:10.108 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: dalvik.system.VMStack.getThreadStackTrace(Native Method)
03-17 04:48:10.108 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: dalvik.system.VMStack
03-17 04:48:10.108 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: VMStack.java
03-17 04:48:10.118 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: -2
03-17 04:48:10.118 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: getThreadStackTrace
03-17 04:48:10.118 701-701/com.loaderman.getthreadstatusdemo I/System.out: ----  the 1 element  ----
03-17 04:48:10.138 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: java.lang.Thread.getStackTrace(Thread.java:591)
03-17 04:48:10.138 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: java.lang.Thread
03-17 04:48:10.148 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: Thread.java
03-17 04:48:10.148 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 591
03-17 04:48:10.148 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: getStackTrace
03-17 04:48:10.148 701-701/com.loaderman.getthreadstatusdemo I/System.out: ----  the 2 element  ----
03-17 04:48:10.148 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: com.loaderman.getthreadstatusdemo.MainActivity.printStackInfos(MainActivity.java:16)
03-17 04:48:10.148 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: com.loaderman.getthreadstatusdemo.MainActivity
03-17 04:48:10.158 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: MainActivity.java
03-17 04:48:10.158 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 16
03-17 04:48:10.168 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: printStackInfos
03-17 04:48:10.168 701-701/com.loaderman.getthreadstatusdemo I/System.out: ----  the 3 element  ----
03-17 04:48:10.168 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: com.loaderman.getthreadstatusdemo.MainActivity.onCreate(MainActivity.java:12)
03-17 04:48:10.168 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: com.loaderman.getthreadstatusdemo.MainActivity
03-17 04:48:10.168 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: MainActivity.java
03-17 04:48:10.168 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 12
03-17 04:48:10.179 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: onCreate
03-17 04:48:10.179 701-701/com.loaderman.getthreadstatusdemo I/System.out: ----  the 4 element  ----
03-17 04:48:10.179 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: android.app.Activity.performCreate(Activity.java:5008)
03-17 04:48:10.188 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: android.app.Activity
03-17 04:48:10.188 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: Activity.java
03-17 04:48:10.198 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 5008
03-17 04:48:10.198 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: performCreate
03-17 04:48:10.198 701-701/com.loaderman.getthreadstatusdemo I/System.out: ----  the 5 element  ----
03-17 04:48:10.208 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-17 04:48:10.208 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: android.app.Instrumentation
03-17 04:48:10.208 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: Instrumentation.java
03-17 04:48:10.208 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 1079
03-17 04:48:10.218 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: callActivityOnCreate
03-17 04:48:10.218 701-701/com.loaderman.getthreadstatusdemo I/System.out: ----  the 6 element  ----
03-17 04:48:10.218 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-17 04:48:10.228 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: android.app.ActivityThread
03-17 04:48:10.228 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: ActivityThread.java
03-17 04:48:10.228 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 2023
03-17 04:48:10.228 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: performLaunchActivity
03-17 04:48:10.228 701-701/com.loaderman.getthreadstatusdemo I/System.out: ----  the 7 element  ----
03-17 04:48:10.238 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-17 04:48:10.238 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: android.app.ActivityThread
03-17 04:48:10.238 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: ActivityThread.java
03-17 04:48:10.238 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 2084
03-17 04:48:10.248 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: handleLaunchActivity
03-17 04:48:10.248 701-701/com.loaderman.getthreadstatusdemo I/System.out: ----  the 8 element  ----
03-17 04:48:10.248 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: android.app.ActivityThread.access$600(ActivityThread.java:130)
03-17 04:48:10.258 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: android.app.ActivityThread
03-17 04:48:10.258 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: ActivityThread.java
03-17 04:48:10.258 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 130
03-17 04:48:10.258 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: access$600
03-17 04:48:10.268 701-701/com.loaderman.getthreadstatusdemo I/System.out: ----  the 9 element  ----
03-17 04:48:10.268 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-17 04:48:10.268 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: android.app.ActivityThread$H
03-17 04:48:10.268 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: ActivityThread.java
03-17 04:48:10.268 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 1195
03-17 04:48:10.268 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: handleMessage
03-17 04:48:10.278 701-701/com.loaderman.getthreadstatusdemo I/System.out: ----  the 10 element  ----
03-17 04:48:10.278 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: android.os.Handler.dispatchMessage(Handler.java:99)
03-17 04:48:10.278 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: android.os.Handler
03-17 04:48:10.278 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: Handler.java
03-17 04:48:10.288 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 99
03-17 04:48:10.288 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: dispatchMessage
03-17 04:48:10.288 701-701/com.loaderman.getthreadstatusdemo I/System.out: ----  the 11 element  ----
03-17 04:48:10.288 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: android.os.Looper.loop(Looper.java:137)
03-17 04:48:10.288 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: android.os.Looper
03-17 04:48:10.288 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: Looper.java
03-17 04:48:10.288 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 137
03-17 04:48:10.288 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: loop
03-17 04:48:10.300 701-701/com.loaderman.getthreadstatusdemo I/System.out: ----  the 12 element  ----
03-17 04:48:10.300 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: android.app.ActivityThread.main(ActivityThread.java:4745)
03-17 04:48:10.308 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: android.app.ActivityThread
03-17 04:48:10.308 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: ActivityThread.java
03-17 04:48:10.308 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 4745
03-17 04:48:10.308 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: main
03-17 04:48:10.308 701-701/com.loaderman.getthreadstatusdemo I/System.out: ----  the 13 element  ----
03-17 04:48:10.308 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: java.lang.reflect.Method.invokeNative(Native Method)
03-17 04:48:10.308 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: java.lang.reflect.Method
03-17 04:48:10.318 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: Method.java
03-17 04:48:10.318 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: -2
03-17 04:48:10.318 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: invokeNative
03-17 04:48:10.318 701-701/com.loaderman.getthreadstatusdemo I/System.out: ----  the 14 element  ----
03-17 04:48:10.318 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: java.lang.reflect.Method.invoke(Method.java:511)
03-17 04:48:10.318 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: java.lang.reflect.Method
03-17 04:48:10.318 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: Method.java
03-17 04:48:10.318 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 511
03-17 04:48:10.318 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: invoke
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: ----  the 15 element  ----
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: com.android.internal.os.ZygoteInit$MethodAndArgsCaller
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: ZygoteInit.java
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 786
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: run
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: ----  the 16 element  ----
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: com.android.internal.os.ZygoteInit
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: ZygoteInit.java
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: 553
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: main
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: ----  the 17 element  ----
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: toString: dalvik.system.NativeStart.main(Native Method)
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: ClassName: dalvik.system.NativeStart
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: FileName: NativeStart.java
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: LineNumber: -2
03-17 04:48:10.328 701-701/com.loaderman.getthreadstatusdemo I/System.out: MethodName: main

可以发现当获取stack trace的语句放在另一个方法(工具类方法)中的时候,数组索引为3的元素可以找到该工具类方法的调用地点。
在此例中,找到的是onCreate()方法中的第12行。


本文学习来源:http://www.jianshu.com/p/71fefdf6d62c


获取当前线程状态--Thread类的更多相关文章

  1. 【转】线程、Thread类和线程终止

    一.线程Thread启动 0. Thread类实现了java.lang.Runnable接口,即实现了run方法.虽然在Sun JDK中,start()调用了start0()方法,start0()方法 ...

  2. 线程状态Thread.State

    线程状态Thread.State 线程状态.线程可以处于下列状态之一: NEW 至今尚未启动的线程处于这种状态. RUNNABLE 正在 Java 虚拟机中执行的线程处于这种状态. BLOCKED 受 ...

  3. 【C# 线程】Thread类 以及使用案例

    System.Threading.Thread类 涉及到的类和枚举 Volatile 类Interlocked 类SpinLock 类SpinWait类Barrier 类ThreadLocal< ...

  4. 几种创建线程方式Thread类和Runnable接口

    对于很多想学习java的人来说,经常听别人说线程难,其实真正理解了线程后,一点都不会觉得线程难,这里我为大家梳理下线程的创建方式吧. 一.线程的创建方式有三种 1.继承Thread类 2.实现Runn ...

  5. java学习之- 线程继承Thread类

    标签(空格分隔): 线程 在java.lang包中有个Thread子类,大家可以自行查阅文档,及范例: 如何在自定义的代码中,自定义一个线程呢? 1.通过对api的查找,java已经提供了对线程这类事 ...

  6. NtQuerySystemInformation获取进程/线程状态

    __kernel_entry NTSTATUS NtQuerySystemInformation( SYSTEM_INFORMATION_CLASS SystemInformationClass, P ...

  7. 手撕代码之线程:thread类简单使用

    转载于:https://blog.csdn.net/qq_22494029/article/details/79273127 简单多线程例子: detch()启动线程: 1 #include < ...

  8. Java多线程01(Thread类、线程创建、线程池)

    Java多线程(Thread类.线程创建.线程池) 第一章 多线程 1.1 多线程介绍 1.1.1 基本概念 进程:进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于 ...

  9. 并发基础(六) 线程Thread类的start()和run()

    start()和run()方法对于刚接触线程的人来说,会有点混淆,有点难理解,一般都会有以下疑问: 一.start( )方法 1.为什么需要start方法:它的作用是什么: start方法的作用就是将 ...

随机推荐

  1. Js 将图片的绝对路径转换为base64编码(3)

    图片文件改变一方法:$('#file').change(function(){var oFReader = new FileReader();oFReader.readAsDataURL(this.f ...

  2. Wayos网吧路由英雄联盟频繁掉线解决办法

    英雄联盟某些机器瞬间ping值飙升,然后一直掉线重连!研究好久,解决了,经验与大家分享 第一步,在路由器地址后加qos_ext.htm进入qos参数设置页面(如果出现不了设置界面请更新固件).比如:h ...

  3. DNS解析综合学习案例(附详细答案)

    1.用户需把/dev/myvg/mylv逻辑卷以支持磁盘配额的方式挂载到网页目录下2.在网页目录下创建测试文件index.html,内容为用户名称,通过浏览器访问测试3.创建用户账户,对LVM配置磁盘 ...

  4. date( ) 日期函数

    date('Y-m-dT2:00')    实际时间为14:00 date('Y-m-d 2:00')     实际时间为2:00 扩展:每天的时间戳秒数为 86400

  5. shell更改xml中的指定值

    sed -i 's;<id>.*<\/id>;<id>新内容<\/id>;g'  your.xml

  6. 第二章 Vue快速入门-- 24 过滤器-Vue中全局过滤器的基本使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  7. 校验 url 是否以http 或者https 开头

    var reUrl01 = /^((ht|f)tps?):\/\/([\w-]+(\.[\w-]+)*\/?)+(\?([\w\-\.,@?^=%&:\/~\+#]*)+)?$/; var r ...

  8. k8sReplicaSet控制器

    一.ReplicaSet概述 简称RS,是pod控制器类型的一种实现,用于确保由其管控的pod对象副本数在任一时刻都能精确满足期望的数量.ReplicaSet控制器资源启动后会查找集群中匹配其标签选择 ...

  9. Kubernetes 基本概念和术语

    Kubernetes 基本概念和术语 Kubernetes 中大部分概念如 Node.Pod.Replication Controller. Service 等都可以看做一种 "资源对象&q ...

  10. [洛谷P4602] CTSC2018 混合果汁

    问题描述 小 R 热衷于做黑暗料理,尤其是混合果汁. 商店里有 n 种果汁,编号为 0, 1, 2, . . . , n − 1.i 号果汁的美味度是 di,每升价格为 pi.小 R 在制作混合果汁时 ...