Appendix A. Miscellaneous Topics

Throughout this book, we have examined the various parts of the threading system. This examination was based on various examples and issues that commonly occur during program development. However, there were certain rather obscure issues that fell through the cracks; these are the topics we will examine in this appendix.

Thread Stack Information

The Thread class provides these methods to supply the programmer with information about the thread’s stack:

int countStackFrames() (deprecated in Java 2)

Returns the number of stack frames in the specified thread. The thread must be suspended in order for this method to work. This is a method of the Thread class and does not count the frames that are from native methods. Since the thread must be suspended, it is not possible to obtain the count for the current thread directly.

static void dumpStack()

Prints the stack trace of the current thread to System.err. This is a static method of the Thread class and may be accessed with the Thread specifier. Only the stack trace of the currently running thread may be obtained.

Interestingly, we might conclude from these two methods that we can both count the number of stack frames and actually print the stack frames out. However, these two methods cannot be used together. Since the thread needs to be suspended in order to count the stack frames, it is not possible to count the frames of the current thread, and the dumpStack() method can only print the stack information ...

Get Java Threads, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.