wood burning stoves 2.0*
The moose likes Java in General and the fly likes Singleton pattern and thread issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Singleton pattern and thread issue" Watch "Singleton pattern and thread issue" New topic
Author

Singleton pattern and thread issue

Steve Mutanson
Ranch Hand

Joined: Apr 15, 2003
Posts: 67
If I use Singleton pattern to create ONE instance in the JVM and let this instance be available to multiple threads. I know it is NOT thread safe. But now my question is --- if I don't do any kind of "update", "write" type thing on this instance, i.e. only use this instance to do some other tasks, for example, in the following example, just do some traverse on the Binary search tree without "inserting" any node. Do I need to worry about the thread problem when multiple threads use the treeRoot ?
*********
public class BinaryTree {
private static BinaryTree treeRoot = new BinaryTree();
public static BinaryTree getInstance() {
return treeRoot;
}
....
}

public class Test {
public void doTask() {
BinaryTree bt = BinaryTree.getInstance();
traverse(bt);
}
..
}
********************
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
If the class is, as you say, read only, then you should have no problem with multiple threads accessing the singleton. I guess my question would be how do you populate the tree and once populated is it static? Even if a single thread is allowed to alter the tree, the only consequence to reading threads would be a chance of reading stale data according to the data structure used for the tree. Now if you are iterating thru a Collection with a reading thread while a writing thread is altering the Collection, then chances are a ConcurrentModificationException will be thrown.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Steve Mutanson
Ranch Hand

Joined: Apr 15, 2003
Posts: 67
Originally posted by Michael Morris:
If the class is, as you say, read only, then you should have no problem with multiple threads accessing the singleton. I guess my question would be how do you populate the tree and once populated is it static? Even if a single thread is allowed to alter the tree, the only consequence to reading threads would be a chance of reading stale data according to the data structure used for the tree. Now if you are iterating thru a Collection with a reading thread while a writing thread is altering the Collection, then chances are a ConcurrentModificationException will be thrown.

Thanks lot for reply. Actually this static singleton instance will just use a tree that is created at once and will not be changed. For example, like the follwoing code --
class BinaryTree {
private static BinaryTree treeRoot = MySystem.getTree("a", "b", "c", "d", "e");
...
}
"MySystem" will create a binary tree consisting of
node "a", "b",.."e". So the treeRoot just holds the reference to this tree.
Guess this makes it bit clearer. So, can I assume it is safe to access this treeRoot by multiple threads now ?
Thanks,
steve
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
So, can I assume it is safe to access this treeRoot by multiple threads now ?
Yep.
Steve Mutanson
Ranch Hand

Joined: Apr 15, 2003
Posts: 67
Originally posted by Michael Morris:
So, can I assume it is safe to access this treeRoot by multiple threads now ?
Yep.

Please allow me to ask a follow-up "silly" question ---
If I need multiple (or as many as 40) different binary trees or other data struture trees for some purpose and I want to use this singleton pattern. The if I write the following --
class Trees {
private static BinaryTree1 = new MySystem.getTree(...);
private static BinaryTRee2 = ....;
....
private BinaryTree20 = ..
private QuadTree1 = new ...
...
private QuadTRee20 = new ..

...
}
Then in my main program, whenever I want to access any one of the trees, I just get that single instance reference. In other words, for each tree, there is only one instance. Now the question is --- Since these instances are created when the "Tree" class is loaded into JVM and it takes time, will it be possible that when I try to access the nth tree say the 10th tree root, this 10th tree has NOT been created in the system yet ? Usually we only put one type of object in the singleton class so we don't worry about it. Do you think I need to worry about it if I have many (say 40) different instances to create ?
Thanks,
steve
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
The JVM doesn't allow any thread to execute code in the class until class initialisation (including static variable initialisers and static blocks) has completed. So you're safe.
- Peter
[ June 10, 2003: Message edited by: Peter den Haan ]
 
 
subject: Singleton pattern and thread issue