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. oracle数据库连接问题org.springframework.jdbc.support.MetaDataAccessException: JDBC DatabaseMetaData method not implemented by JDBC driver - upgrade your driver...

    org.springframework.jdbc.support.MetaDataAccessException: JDBC DatabaseMetaData method not implement ...

  2. mysoft

    @@a8649fbb56349908b5ca6708fb94b3ddabcf6b97381a9797d3dfb139b8749287117@@##74e02e1207e5a0a8996ba89f1d6 ...

  3. 韦东山嵌入式Linux学习笔记04--点亮开发板的一个LED灯

    搜索开发板原理图LED的走线           LED8是网线接口的指示灯. 在这里我们尝试用汇编代码控制D10, 也就是LED1,它连接到EINT4/GPF4,读取芯片手册 有原理图可知,如果需要 ...

  4. 安装vim自动补全插件

    1 安装VIM 2 安装vim插件管理工具.过程见链接.(谢谢) 3 在.vimrc中添加下列代码 Bundle 'Valloric/YouCompleteMe' 保存退出后打开vim,在正常模式下输 ...

  5. 单节点oracle、ASM 详细安装步骤

    目录 1.安装环境 2.系统要求 2.1 Linux安装Oracle系统要求 1.查看RAM和交换空间以及磁盘大小 2.检查所需软件包 3.配置host和主机名 2.2修改操作系统核心参数 1.创建相 ...

  6. filebeat收集nginx的json格式日志

    一.在nginx主机上安装filebeat组件 [root@zabbix_server nginx]# cd /usr/local/src/ [root@zabbix_server src]# wge ...

  7. RPM包搭建

    打包rpm软件包之spec文件解析 1. 概述 RPM的全称是(Red Hat Package Manager,Red Hat包管理器).RPM是一个开放的软件包管理器,工作在Red Hat.类Lin ...

  8. Codeforces Round #344 (Div. 2) 631 B. Print Check (实现)

    B. Print Check time limit per test1 second memory limit per test256 megabytes inputstandard input ou ...

  9. 关于ORACLE事务处理的一些笔记

    这是2013年在看ORACLE概念手册的时候的一些笔记,现在整理如下(可能跟其他一些文章的内容有重复):     20131012 周六 oracle概念手册中文版 第4章 事务管理   事务具有原子 ...

  10. feign请求远程接口时报Caused by: java.net.UnknownHostException

    报错全文如下 feign.RetryableException: host10.local executing POST http://TIBMAS2-WEBAPI/tibmas2-webapi/ap ...