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

  • Home
  • SEARCH
  • 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 7876579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:11:20+00:00 2026-06-03T03:11:20+00:00

As far as I can understand from the java.lang.Thread docs, and from other questions

  • 0

As far as I can understand from the java.lang.Thread docs, and from other questions posted here on stackoverflow, like “How to access a Runnable object by Thread?” and “Get current instance of Runnable” it’s not possible to get a reference of the Runnable object embedded in a Thread.

A scenario where having this possibility would be useful is when implementing a method, whose signature can’t be modified because we are overriding a method defined in another class or interface, and different operations are required to be performed depending on the type of Runnable embedded in the current Thread.

If we had for example a method called getRunnable in the Thread class we could do something like this:

if (Thread.currentThread().getRunnable() instanceof Type1) {
  // do something...
} else {
  // do something else...
}

This could be useful also in situations when we want to ensure the operations contained in a method are executed only by certain threads and not by others.

So I was wondering, is there a specific reason why the Java developers decided to not allow to get the Runnable instance from a Thread, or is it be a missing feature worth to be notified? If you think there is no reason behind that choice but it’s not worth to notify it as missing feature, what strategy would you use in the scenario described above?

  • 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-06-03T03:11:21+00:00Added an answer on June 3, 2026 at 3:11 am

    So I was wondering, is there a specific reason why the Java developers decided to not allow to get the Runnable instance from a Thread

    It was probably just not a requirement. The Runnable itself should be able to identify its own class so the idea that it needs to get that information is strange. It could also be a protection so that other threads would not have access to the class being running in another thread.

    If you need access to the current Runnable from other parts of your application then I’d recommend using a ThreadLocal<Runnable>. In your run() method you could set it and then retrieve it in your other classes. You’d need to put the ThreadLocal somewhere globally accessible however.

    You could also process the current stack trace to figure out the enclosing Runnable class which is even more of a hack but it would work.

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

Sidebar

Related Questions

So far I can understand HTML.ActionLink and URL.RouteURL but still trying to absorb where
As far as I can understand, when I new up a Linq to SQL
I'm new to Java, and while reading documentation so far I can't find any
As far as I understand JRuby, it is perfectly possible to use Java class
As far as I can understand streams are of a fixed size, whether they
I am working on a contact list project and so far i can add,modify
Working on a carousel lightbox tooltip demo and as far I can see in
I'm writing a program to generate some wild visuals. So far I can paint
As far as I can tell, this is isn't possible, so I'm really just
As far as I can tell, the best way to do this is do

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.