File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes The use of Hashtable for synchronization. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "The use of Hashtable for synchronization." Watch "The use of Hashtable for synchronization." New topic
Author

The use of Hashtable for synchronization.

Dieter Quickfend
Ranch Hand

Joined: Aug 06, 2010
Posts: 359
from ExamLab:

Suppose you have to make a student details sheet from the Java Collections Framework. There should be two fields, one is studentID, and the other is studentName. Your program is multi-threaded, and every method in your program should be threadsafe. Which of the following classes should you use to perform the above task (Choose one)

A. LinkedList
B. TreeSet
C. Vector
D. Hashtable
E. LinkedHashSet
F. TreeMap
G. HashMap
H. LinkedHashMap
I. Nothing can be used.






My answer was TreeMap, the correct answer was Hashtable.

Now, I realize that the methods of Hashtable are synchronized while TreeMap's methods aren't, but I really don't think that this is the right class to use for this program. The question says that every method in your program should be threadsafe, not every method in the collection you're using. The reason, I thought, that Hashtable and Vector are deprecated, is that they don't make your application threadsafe, it's all about how you code your application.

Then I doubted between HashMap for performance and TreeMap for utility, but because I assumed that in this case, sorting order could actually come in handy, I chose TreeMap. I thought that it was a toss-up between those two classes, but I hadn't counted on Hashtable... I really wouldn't use that one if my life depended on it.

... So why is Hashtable the method I should use for this program?


Oracle Certified Professional: Java SE 6 Programmer
Oracle Certified Expert: Java EE 6 Web Component Developer
Oracle Certified Expert: Java EE 6 Enterprise JavaBeans Developer
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Dieter Quickfend wrote:...Your program is multi-threaded, and every method in your program should be threadsafe...

Making the collection you use synchronized is far more better than making all the methods in your code synchronized. Collections are not synchronized at all(thats why they give off good performance) - that eliminates all the Collection classes in your options, leaving behind 'Vector' and 'HashTable'. Of which 'HashTable' is the only one that can store a key-value pair.
Dieter Quickfend wrote:The reason, I thought, that Hashtable and Vector are deprecated, is that they don't make your application threadsafe, it's all about how you code your application.

The Legacy classes are by default synchronized. Though 'Legacy' may imply that they must not be used in your code, they are not deprecated actually.


OCPJP 6
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

A common interpretation is that 'Deprecated' means that it will be removed in the near future, and 'Legacy' means that it will remain for backwards compatibility or other reasons.
Both mean that they shouldn't be used by new code.
In the case of the JDK even Deprecated code will remain, since backwards compatibility is very important for the Java JDK.


This is what I found out when googled.
Dieter Quickfend
Ranch Hand

Joined: Aug 06, 2010
Posts: 359
Vinoth Kumar Kannan wrote:
Dieter Quickfend wrote:...Your program is multi-threaded, and every method in your program should be threadsafe...

Making the collection you use synchronized is far more better than making all the methods in your code synchronized. Collections are not synchronized at all(thats why they give off good performance) - that eliminates all the Collection classes in your options, leaving behind 'Vector' and 'HashTable'. Of which 'HashTable' is the only one that can store a key-value pair.


But a synchronized collection will not make your program thread safe. And Vector is a List is a Collection and is synchronized.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

from all of the opions given only vector and hashtable methods are synchronized
As we have to store key and values,so hashtable should be choice.


SCJP6.0,My blog Ranchers from Delhi
Dieter Quickfend
Ranch Hand

Joined: Aug 06, 2010
Posts: 359
Yes, I got that far, but why not use another Map? Hashtable will not make a program threadsafe, you have to code in threadsafety yourself, at a higher level.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

As per the question HashTable is the appropriate answer but while in real life we can
use MAp to because HashTable will not make the class as ThreadSafe untill we make all the methods as synchronized
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Shanky Sohar wrote:As per the question HashTable is the appropriate answer but while in real life we can
use MAp to because HashTable will not make the class as ThreadSafe untill we make all the methods as synchronized


Hashtable is synchronized! Check the API


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
Abimaran Kugathasan wrote:
Hashtable is synchronized! Check the API


I think Shanky was talking about the methods of Class in which the hashtable will be modified.

Regards,
Avishkar Nikale
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Avishkar Nikale wrote:
Abimaran Kugathasan wrote:
Hashtable is synchronized! Check the API

I think Shanky was talking about the methods of Class in which the hashtable will be modified.

Anyway, since all the Hashtable's methods(get,put,clear....) are already synchronized, we need not guard them with our own synchronization.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Abimaran Kugathasan wrote:
Hashtable is synchronized! Check the API


read my above posts....i already mention that
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051


i need to say that even though method of hashtable is synchronized we cannot say that complete class which use HashTable is ThreadSafe.
To make a whole class as ThreadSafe we have to make each and every method of a class as synchronized.
Divyeshh Patel
Ranch Hand

Joined: Aug 03, 2010
Posts: 37
I fully agree with Dieter.
I think that the question is wrongly worded.
Nevertheless, the intention of this question is just to test your knowledge on which synchronized class is available to store key-value pairs.
Otherwise we can write synchronized code using all the classes mentioned(by creating an object and using that object to synchronize the program's code)
I might use 2 Vector objects, one to store studentId and one to store studentName


Cheers,
Divyesh.
Dieter Quickfend
Ranch Hand

Joined: Aug 06, 2010
Posts: 359
Honestly, I would just imagine that a Student can have much more content than ID and name, and use ArrayList<Student> for the job.

Yet the fact of the matter remains is that a programmer coding such an application would not benefit from using Hashtable, even if you only needed a key value pair. Both HashMap and TreeMap are far more eligible.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Dieter Quickfend wrote:Honestly, I would just imagine that a Student can have much more content than ID and name, and use ArrayList<Student> for the job.

Yet the fact of the matter remains is that a programmer coding such an application would not benefit from using Hashtable, even if you only needed a key value pair. Both HashMap and TreeMap are far more eligible.


As i have already mention that,As per the Question given your choice should be hashtable.This Question is meant to test your knowledge whether you know which collection methods are synchronized or not.

Array list will not store key value pair.

In real life,there are so many choices you can have but here for a correct answer you have to select HAshTable only.

Why we cannot use the hashmap and treemap is..
1. TreeMap always sort the value in natural order but as per question there is not sorting is required then why make our code too do the sorting and decrease it performance.
2.HashMap allows one null key but it is not possible that student Id will be null....but if any person forget to enter any key then press enter then system will take null as his ID.

HashTable is synchronized and all its methods are also..and it also doesnot allow any null key in it

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The use of Hashtable for synchronization.
 
Similar Threads
what is the use of different collections..
Hash Table - Sychronization
HashMap or HashTable
new SCJP in town :)
hashpmap and dictionary and hashtable?