permaculture playing cards*
The moose likes Threads and Synchronization and the fly likes Can a class be singleton after implements Runnable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Can a class be singleton after implements Runnable?" Watch "Can a class be singleton after implements Runnable?" New topic
Author

Can a class be singleton after implements Runnable?

Abu Nene
Ranch Hand

Joined: Nov 13, 2008
Posts: 56
Any concern if I do this



Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Abu,

in general any class can implement an interface like "Runnable".

The question is what should the code in this example do? Or better what do you expect it to do? Currently you create a class which implements Runnable but isn't really used as a Runnable and you have a start() method which starts off a new thread that does not very much. I guess that's not what you intended it to do

Marco
Abu Nene
Ranch Hand

Joined: Nov 13, 2008
Posts: 56
Marco Ehrentreich wrote:Hi Abu,

in general any class can implement an interface like "Runnable".

The question is what should the code in this example do? Or better what do you expect it to do? Currently you create a class which implements Runnable but isn't really used as a Runnable and you have a start() method which starts off a new thread that does not very much. I guess that's not what you intended it to do

Marco


Hi Marco,

My intention is to create a new thread by using a singleton. For eg:

Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Typical singletons are in most cases considered as bad design today and I still don't see what you're trying to achieve with this. Sorry

What does that mean "create a new thread by using a singleton"? I guess you're confusing something here regarding Threads and Runnables. Perhaps you should just describe a little more what you expect this singleton to do.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38088
    
  22
And have the same object running in two different threads?

No longer a beginner's question. Moving.
Abu Nene
Ranch Hand

Joined: Nov 13, 2008
Posts: 56
Marco Ehrentreich wrote:Typical singletons are in most cases considered as bad design today and I still don't see what you're trying to achieve with this. Sorry

What does that mean "create a new thread by using a singleton"? I guess you're confusing something here regarding Threads and Runnables. Perhaps you should just describe a little more what you expect this singleton to do.


My intention is to used this singleton class to invoke a thread which will create instance of other classes
Abu Nene
Ranch Hand

Joined: Nov 13, 2008
Posts: 56
Campbell Ritchie wrote:And have the same object running in two different threads?

No longer a beginner's question. Moving.


Yes and this class will use the Reflection API to instantiate other classes. Any concern on this?
Ajay Saxena
Ranch Hand

Joined: Nov 13, 2006
Posts: 154
Why don't you just tell us why you decided to go for singletons in the first place? In other words,tell us more about what (functionality) you intend to achieve from a high level without getting into singletions and threads for the moment.
Abu Nene
Ranch Hand

Joined: Nov 13, 2008
Posts: 56
Ajay Saxena wrote:Why don't you just tell us why you decided to go for singletons in the first place? In other words,tell us more about what (functionality) you intend to achieve from a high level without getting into singletions and threads for the moment.


The reason why I go for singleton is because I see that only one instance is required to create new threads.



In test1, there is only one instance of MyClass to create new threads but in test2 method, a new instance of MyClass is created to create new threads.

Do correct me if I'm wrong, thanks!
Ajay Saxena
Ranch Hand

Joined: Nov 13, 2006
Posts: 154
In test1, there is only one instance of MyClass to create new threads but in test2 method, a new instance of MyClass is created to create new threads.


Hold on..You are mixing up concepts here - singletons with runnable objects

The idea behind creating a thread object with a runnable object passed to its class constructor (the pattern you follow in both test1 and test2) is to execute the task logic encapsulated in the run() method of the runnable object in a different thread.So there's certainly nothing much you gain out of reusing the same runnable object across multiple threads except perhaps under extremely tricky situations that mandate such usage.So you would probably ,in most cases,want different runnable objects to be handed off to different threads.

Talking about singletons,they are used in situations where it's expensive to create multiple objects of the same class ,where it doesn't make sense to have multiple objects. A typical example could be a DBDonnectionFactory or a DBConnectionPoolManager.

Do some research on the diversity of applications of the singleton pattern.There's a wealth of information available. Also you may want to revisit the tutorials on concurrent programming in java.
 
jQuery in Action, 2nd edition
 
subject: Can a class be singleton after implements Runnable?
 
Similar Threads
inner question
jcert thread question
Please explain the concept of private constructor
Confusion in threads
[B]Class Monitor Vs. Object Monitor[/B]