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 3932872
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:32:17+00:00 2026-05-19T23:32:17+00:00

I’m writing a Java program involving a multithreaded worker pool of Process es. Each

  • 0

I’m writing a Java program involving a multithreaded worker pool of Processes. Each instance of the class Process needs to be able to spawn a single additional thread to do some work. But the thread should by spawned by the instance itself and nobody else. Unfortunately Runnable.run is ‘public’ so I cannot really enforce this without doing some trick.

Here is the trick I plan to use:

  1. Implement Runnable in Process
  2. Write this into the implementation:

Code:

 class Process implements Runnable {

    private boolean threadkey = false;

    public void run() {

        synchronized(threadkey) {

            // is someone wrongly calling 'run'?
            if(!threadkey)
                return;

            /* switch off threadkey to make sure
            it cannot be called meaningfully again */
            threadkey = false;
        }
        /* continue processing
         *
         */
        return;
    }

Of course, now all I will need to do when I want to run legitimately is to switch on ‘threadkey’ (which is private) before making the call.

Elegant? Or not? Or is there a better way? Or should I just not bother with enforcing this and write a neat little comment explaining NOT to call ‘run’?

Do people even call ‘run’ from within the class that needs to do the ‘running’?

  • 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-19T23:32:17+00:00Added an answer on May 19, 2026 at 11:32 pm

    While you are correct that the run method of a Runnable is public, one thing you can do to prevent other people from calling it is to make your entire Runnable implementation a package-private class or a private inner class. This way, while it may have a public run method, code other than your custom class cannot instantiate the Runnable object. If you also have your class not hand back references to it, then clients won’t have access to the Runnable, and so they can’t invoke run. That is, don’t have your Process class implement Runnable; instead, make another class that actually implements Runnable, then make Process the only class that can access it.

    This approach is more elegant than what you have proposed because it prevents other code from calling run at compile-time rather than at runtime. In particular, if any code tries to call your run method described above, it will compile just fine, and will fail at runtime. The problem is that the code will compile but can never work correctly. Making the Runnable inaccessible means that if someone does try to run it, they’ll get a compile-time error and will have to rethink their design. In other words, the compiler will detect the error before it even happens.

    In general, if you ever find yourself wanting to prevent random code from calling methods of a class that have to be public because they’re declared in an interface, consider changing the access specifier for the entire class so that clients can’t reference it.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

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.