0

I have a singleton class that is shared by some threads.

within a method of this singleton i want to create another thread to do some job (so any thread that uses this singleton can start the additional thread job)

Right now I start threads in simple way:

mSThread = new Thread(job that implements Runnable);
mSThread.start();

Thread mSThread is declared as class member, I don't think that I need to keep reference to the threads so it's ok that every time a new thread is created the reference will be lost

Is it ok to do what i did or i should use a different technique like thread pool?

3
  • 1
    I don't see what having a singleton has to do with it. You have a method, and anybody that invokes it will spawn a new thread that will run until it's done. That's fine as long as you don't end up calling it so often, that you create too many threads. In such cases, yes a thread pool is better. But you provide too little information to give good advice, so please elaborate a bit. Commented Dec 14, 2011 at 18:59
  • Why don't you just do: (new Thread(job that implements Runnable)).start(); in your method? That should do it. Commented Dec 14, 2011 at 19:01
  • thanks, but i don't know what else should I add :), the maximum number of spawns is 20-30... Commented Dec 14, 2011 at 19:02

4 Answers 4

3

It is not absolutely necessary to keep a reference to the thread object, so if you don't need it for anything else, you don't need to store it in a member variable; you can just start the thread and forget the reference.

Whether you should use a thread pool, depends on what exactly your application does, how often you expect to start new threads, etc.; without further information it's hard to say whether this is worth it or not. If you do this, you'll want to use the classes from the java.util.concurrent package. Using an ExecutorService you can start background tasks in a thread pool.

Sign up to request clarification or add additional context in comments.

Comments

1

Threads are a scarce resource of the runtime. Spawning a new one every time your method is called is wasteful - especially since in your case it seems that all you care for is for the calling thread not to be blocked. So, you should make sure that your method does not get called too often.

For now, from the comments, you seem to have done that. You should also document the expectation that your method should not be called too often. Yet, some future colleague can accidentally miss this documentation and call it in a tight loop.

To prevent such occurrences, you might want to consider using a thread pool - it prevents future accidental misuse - especially since Sun/Oracle has provided the awesome java.util.concurrent package which abstract away most of the messy details.

Comments

0

It's ok in my opinion.

What you're really facing here is possibly a premature optimization question. How often will threads be created? Will your app ever become busy enough that this becomes a problem? On your target platform how much resources and time does it take to create a new thread and is this acceptable?

Another questions is do you want to keep track of the number of threads created for management (JMX) purposes?

Comments

0

Thread mSThread is declared as class member,

Do you mean it is an instance variable of the singleton? If so you may have problems. Better to make it a local variable.

2 Comments

it's variable of the singleton but i don't use it
The problem with having it as an instance variable, whether you use it or not, is that if a second invocation of the method occurs between the creation of the tread and the call to start it then you may end up with a new thread that is not started and a thread that is started twice!! Better to use new Thread(job that implements Runnable).start(); as mentioned elsewhere by @eboix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.