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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:25:24+00:00 2026-06-04T06:25:24+00:00

If I have a class that implements java.lang.Runnable , there is technically nothing stopping

  • 0

If I have a class that implements java.lang.Runnable, there is technically nothing stopping multiple threads from calling run concurrently on the same instance of the Runnable. Example:

Runnable r = new MyRunnable();

//start thread 1
new Thread(r).start();

//start thread 2
new Thread(r).start();

This is not really a problem if the run method is entirely “self-contained” (i.e. doesn’t rely on any private instance members), because there are no variables shared between threads, so your Runnable can never get a “corrupted” internal state. But I think in most cases, your Runnable will have some private instance members that it manipulates in run, so what to do?

To use an example, my MyRunnable class has a private mode field. The primary purpose is to cleanly stop the run method (by setting mode to STOPPED), but it has other uses as well.

public static class MyRunnable implements Runnable
{
    enum Mode { RUNNING, PAUSED, STOPPED, FASTFORWARDING, REWINDING; }

    private volatile Mode mode;

    @Override
    public void run() 
    {
        mode = Mode.RUNNING;
        while (mode != Mode.STOPPED)
        {
            //do stuff. possibly other changes to the mode.
        }
    }

    public void setMode(Mode mode)
    {
        this.mode = mode;
    }

    public Mode getMode()
    {
        return mode;
    }
}

As you can see, if I execute multiple threads concurrently for the same instance ofMyRunnable (like shown earlier), any change to the mode could affect concurrently running threads in different ways. For example, if a thread sets the mode to STOPPED, another thread might change the mode to something else before every other thread even gets to see that it was set to STOPPED. This could easily prevent threads from exiting the while loop.

So how do I design a Runnable to be “concurrently usable”? Is it even possible? Is it not worth it? Is there something like C#’s ThreadStatic attribute I can use (which I think would be perfect in this context)?

  • 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-04T06:25:25+00:00Added an answer on June 4, 2026 at 6:25 am

    It is much more common to construct multiple instances of your MyRunnable in this scenario as opposed to protecting them against multiple use:

    //start thread 1
    new Thread(new MyRunnable()).start();
    //start thread 2
    new Thread(new MyRunnable()).start();
    

    The whole purpose of having multiple threads working is to achieve good parallel computing. If the threads need to lock common resources (like work queues, etc.) then fine but writing your Runnables to make them “concurrently usable” should not be done as a matter of course — there should be good reasons to do so.

    Is it even possible?

    Certainly it is possible. Threads share resources all of the time between them. However in your case with the Mode mode variable, this should be done with multiple instances of your Runnable class.

    If multiple threads are calling common code, one pattern you might consider is using ThreadLocal to store per-thread information. But storing fields inside of multiple instances of your MyRunnable will be more efficient than having one MyRunnable instance with the mode stored in a ThreadLocal.

    Is it not worth it?

    It is certainly not worth it if this is done on principle. Again, the whole point of forking the threads is to have them be as separate as possible from other threads to utilize multiple processores.

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

Sidebar

Related Questions

I have a class Something that implements ISomething . How can I convert/cast from
I have a class that implements the IDisposable interface. I am using a webclient
I have a class that implements properties in a specific way, to handle some
I have a class that implements IComparable. public class MyClass : IComparable<MyClass> { public
I have a class that implements InvocationHandler as below: public class MyProxyClass implements InvocationHandler
Context: I have a class that implements a Session, maintaining a TCP connection to
Given: I have an interface. I have only class that implements that interface. Question:
I have an abstract class that implements IDisposable, like so: public abstract class ConnectionAccessor
I have a class A that implements IEquatable<>, using its fields (say, A.b and
I have a class in .NET that implements IXmlSerializable. I want to serialize its

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.