Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can a class be singleton after implements Runnable?

 
Abu Nene
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any concern if I do this



 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And have the same object running in two different threads?

No longer a beginner's question. Moving.
 
Abu Nene
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic