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:
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.
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 ?
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 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.
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".
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?
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.
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.
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.
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..
subject: is it possible to create a singleton thread class?