File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes is it possible to create a singleton thread class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "is it possible to create a singleton thread class?" Watch "is it possible to create a singleton thread class?" New topic
Author

is it possible to create a singleton thread class?

s ravi chandran
Ranch Hand

Joined: Dec 20, 2010
Posts: 154
hi,

I have a problem. I have a class Target which extends thread. Now there are few classes which will be accessing this class. But the limitation is that all the other classes should get the same instance of the Target class. In actual implementation, Target has some process due to which if I don't use Thread in Target, then this class will block all the other services from the main class of the project.

I have created a sample example utilizing the same setup. I have a class Target which extends Thread, it is used by classes Operator and Operator2. Now the sequence should be like this. If any operator starts first, it starts publishing a text after every few intervals. Now, when the second operator class starts, it should change text value of the first operator class also.. I am providing the code below:









Is this possible?

Thanks
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1506
    
    5

Hi,

Yes. It is possible to have a singleton thread class. But note that once you make your own thread class as singleton, you will be able to create only one instance of it and hence there will be only one thread (of that type).

Further to that, the class Target you have wrote is not singleton.

Since you are calling getTarget method in your code, it will return only one instance (during all subsequent calls to getTarget, it will return the same object) and hence it appears as if class Target is singleton.

But it does have a public constructor, and one can create as many objects as he wants. Also, all those objects can have different counts (since count is non-static). Since Target implements Runnable, once we get more objects of type Target, we can create more threads as well. If this is what you want to do, it is absolutely fine(depending on your design/requirements). But then, it is not a singleton class.

A better design would be to have a private constructor, and a public static method returning the new object (if no object exist) or already created object (so there won't be second object). I guess in your code, all you'll have to do is make the constructor as private.

Hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
s ravi chandran
Ranch Hand

Joined: Dec 20, 2010
Posts: 154
Thanks for replying.. I have made the constructer as private.. I am now accessing it with Target.getTarget() method.. but even now I am getting the same result..
I will check with count as static.. is there any thing else that I missed ?

Thanks
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1506
    
    5

s ravi chandran wrote: but even now I am getting the same result

What do you mean by same result?

Can you please put your modified code here? Because all I can see is a class being treated as a singleton, and two different classes having psvm(public static void main) methods. That's it. Nobody's creating threads, starting those etc. Your modified code would help me to understand exact issue.

Btw, I was wrong about 'only one thread' thing. Even if the class is singleton, same instance can be passed to multiple threads and then those threads can be started.
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 416
    
    2

Sorry I'm only scanning this but ....



shouldn't this be synchronized ?

"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Chris Hurst wrote:Sorry I'm only scanning this but ....



shouldn't this be synchronized ?


If one is doing lazy instantiation as he is, then, yes, it must be synced. A better approach, however, is simply to use eager instantiation. There's never a good reason to do lazy instantiation of a singleton.
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 416
    
    2

I agree with Jeff ;-)

Not sure 'never' but I would have said do eager then move to lazy (much rarer use case not applicable here) if you have to, preferably after you've run a profiler.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Chris Hurst wrote:I agree with Jeff ;-)

Not sure 'never'


There might be some cases in a specialized environment--card, phone, etc.--where there would be a good reason for it. I can't imagine what it would be, but since I don't know anything about those environments, I'll allow for the possibility. In an SE or EE environment though, I stand firm by "never".
s ravi chandran
Ranch Hand

Joined: Dec 20, 2010
Posts: 154
okay, I will take the points one by one. FIrst the changed code. This is the code after change.








ok, so this is the current code. changed a little bit. and by the same result i mean, when both the operators are running, they are showing their own individual text value, what i want is that the second operator class should change the text value of the first running operator.

and now about the synchronization part. That one i completely forgot, to be honest.. and sorry for asking noob question.. but this eager instatiation and lazy loading is looking very confusing to me.. how can I try eager instantiation here?

Thanks

Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2376
    
  28

By any chance, are the 2 Operators running in 2 differrent JVMs? Singletons work only within a JVM. If you have 2 JVMs, you will have 2 singletons.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

s ravi chandran wrote:but this eager instatiation and lazy loading is looking very confusing to me.. how can I try eager instantiation here?



s ravi chandran
Ranch Hand

Joined: Dec 20, 2010
Posts: 154
Jayesh A Lalwani wrote:By any chance, are the 2 Operators running in 2 differrent JVMs? Singletons work only within a JVM. If you have 2 JVMs, you will have 2 singletons.

No, this example, as well as the actual application will be running on same JVM only. all these classes will always be local to each other...
s ravi chandran
Ranch Hand

Joined: Dec 20, 2010
Posts: 154
Jeff Verdegan wrote:
s ravi chandran wrote:but this eager instatiation and lazy loading is looking very confusing to me.. how can I try eager instantiation here?





Thanks Jeff for showing the eager instantiation implementation. I had one doubt though, will it impact the result which I am getting?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

s ravi chandran wrote:
Thanks Jeff for showing the eager instantiation implementation. I had one doubt though, will it impact the result which I am getting?


No. The behavior is identical.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2376
    
  28

s ravi chandran wrote:
Jayesh A Lalwani wrote:By any chance, are the 2 Operators running in 2 differrent JVMs? Singletons work only within a JVM. If you have 2 JVMs, you will have 2 singletons.

No, this example, as well as the actual application will be running on same JVM only. all these classes will always be local to each other...


In your example, both operators have their own main function. That means they will run in separate JVMs.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jayesh A Lalwani wrote:
s ravi chandran wrote:
Jayesh A Lalwani wrote:By any chance, are the 2 Operators running in 2 differrent JVMs? Singletons work only within a JVM. If you have 2 JVMs, you will have 2 singletons.

No, this example, as well as the actual application will be running on same JVM only. all these classes will always be local to each other...


In your example, both operators have their own main function. That means they will run in separate JVMs.


Not necessarily, but that is most likely.

@ravi: Are you executing something like java Operator AND java Operator2? If so, then, as Jayesh points out, you DO have two separate VMs. The only ways it's a single VM is if you only execute the java command once.
s ravi chandran
Ranch Hand

Joined: Dec 20, 2010
Posts: 154
okay, that i did not know. so, running a main() method will cause the program to run in seperate JVM? I tried running two instances of Target from Operator class, but the second instance is not getting called.

This is the code :

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

s ravi chandran wrote:okay, that i did not know. so, running a main() method will cause the program to run in seperate JVM?


No. Please read what I wrote in my previous post.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2376
    
  28

s ravi chandran wrote:okay, that i did not know. so, running a main() method will cause the program to run in seperate JVM? I tried running two instances of Target from Operator class, but the second instance is not getting called.

This is the code :



Most likely because your Thread is a singleton too. I've not tested this but I think if you start a Thread that is already executing, the second call will fail.

What you want to do is just make the text as singleton and create a Thread object for every thread you want to execute.
s ravi chandran
Ranch Hand

Joined: Dec 20, 2010
Posts: 154
okay guys, I have understood what you people were trying to say. My process ( brain) also suffer from lazy instantiation, so I was not able to understand the points before.. Today I tried it again, patiently. Now I have changed the code, I am using eager instantiation now, and also calling the thread object from a single main method. I have also added a protection for thread.start(), so the method wont be called twice, if it is already running.

I will try to use the knowledge I gained from this example, I even think I can now improve my original design to some extent and make it more clearer and efficient.

Thanks to all of you..

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is it possible to create a singleton thread class?