Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6033681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:31:08+00:00 2026-05-23T05:31:08+00:00

Here’s an unusual one: is it possible to obtain the class/method that originally spawned

  • 0

Here’s an unusual one: is it possible to obtain the class/method that originally spawned the currently running thread? Running a stack trace will naturally stop at the top of the call stack for the current thread.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:31:08+00:00Added an answer on May 23, 2026 at 5:31 am

    Yes, you can:
    I offer you 2 ways: one standard and one semi-hack.

    Most of the answers go overboard while it’s supposed to be built-in function in Java.

    Install a security manager and override SecurityManager.getThreadGroup(), you can get the stack trace easily, optionally you can disable the rest of the secutiry checks by overriding the rest of the methods too.

    Hacky one: install an InheritableThreadLocal in the main thread (the one named main and run by method main(String[] args)). Override the protected InheritableThreadLocal.childValue(T parentValue) and you are done.

    Note: you get the stacktrace of the thread being created and the parent thread (reference) but that should be enough to trace issues.

    I decided to write the super simple sample to illustrate how easy it is:
    Here you can see the results. Looking at the sample, I guess that the most elegant solution I have ever posted on this site, mostly b/c it’s non-obvious but simple and smart.

    package bestsss.util;
    
    import java.util.Arrays;
    
    public class StackInterceptor extends InheritableThreadLocal<StackTraceElement[]>{
        public static final StackInterceptor instance;
        static{
            instance = new StackInterceptor();
            instance.set(new Throwable().getStackTrace());
        }
    
        @Override
        protected StackTraceElement[] childValue(StackTraceElement[] parentValue) {
            return new Throwable().getStackTrace();
        }
    
        //test//
        public static void main(String[] args) {
            Runnable r= new Runnable(){
                @Override
                public void run() {
                    System.out.printf("%s - creation stack: %s%n", Thread.currentThread(), Arrays.toString(instance.get()).replace(',', '\n'));
                }           
            };
    
            Thread t1 = new Thread(r, "t1");
            //spacer
            Thread t2 = new Thread(r, "t2");
            t1.start();
            t2.start();     
        }
    }
    
    Thread[t1,5,main] - creation stack: [bestsss.util.StackInterceptor.childValue(StackInterceptor.java:13)
     bestsss.util.StackInterceptor.childValue(StackInterceptor.java:1)
     java.lang.ThreadLocal$ThreadLocalMap.(ThreadLocal.java:334)
     java.lang.ThreadLocal$ThreadLocalMap.(ThreadLocal.java:242)
     java.lang.ThreadLocal.createInheritedMap(ThreadLocal.java:217)
     java.lang.Thread.init(Thread.java:362)
     java.lang.Thread.(Thread.java:488)
     bestsss.util.StackInterceptor.main(StackInterceptor.java:25)]
    Thread[t2,5,main] - creation stack: [bestsss.util.StackInterceptor.childValue(StackInterceptor.java:13)
     bestsss.util.StackInterceptor.childValue(StackInterceptor.java:1)
     java.lang.ThreadLocal$ThreadLocalMap.(ThreadLocal.java:334)
     java.lang.ThreadLocal$ThreadLocalMap.(ThreadLocal.java:242)
     java.lang.ThreadLocal.createInheritedMap(ThreadLocal.java:217)
     java.lang.Thread.init(Thread.java:362)
     java.lang.Thread.(Thread.java:488)
     bestsss.util.StackInterceptor.main(StackInterceptor.java:27)]
    

    Good luck and happy hacking.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's what I'm trying to accomplish with this program: a recursive method that checks
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here's a coding problem for those that like this kind of thing. Let's see
Here is the scenario: I'm writing an app that will watch for any changes
here is a link to how my APC is running : [removed] As you
Here's a query that works fine: SELECT rowid as msg_rowid, a, b, c FROM
here i want to understand the architecture of bluez (Bluetooth Stack Protocol). I understood
Here is my code (Say we have a single button on the page that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.