Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Few java thread  and collection questoins

Thread view: 
puzzlecracker - 29 Jan 2006 17:07 GMT
A. what is the difference between Hashtable and HashMap and when would
you use in instead of the other. It seems to me that HashMap should
never be used for it provides same functionality as hashtable but
slower.

B. Is it possible all running threads in the  jvm? to make it more
concrete:  I have main thread that spawns  many child threads. A child
thread would like to know all threads spawned by the father,

Thanks
Sanjay - 29 Jan 2006 17:28 GMT
> A. what is the difference between Hashtable and HashMap and when would
> you use in instead of the other. It seems to me that HashMap should
> never be used for it provides same functionality as hashtable but
> slower.

All the methods in Hashtable are synchronized and they arent in hash
map.
I guess if you dont have a contention then dont use Hashtable as it
would slower the execution.

> B. Is it possible all running threads in the  jvm? to make it more
> concrete:  I have main thread that spawns  many child threads. A child
> thread would like to know all threads spawned by the father,

You can always do this programmatically while spawning threads or use
ThreadGroup

> Thanks

Sanjay
Sanjay - 29 Jan 2006 17:36 GMT
> > A. what is the difference between Hashtable and HashMap and when would
> > you use in instead of the other. It seems to me that HashMap should
[quoted text clipped - 12 lines]
> You can always do this programmatically while spawning threads or use
> ThreadGroup

I was wrong in saying programatically, I was only thinking about the
parent thread here.
You can use ThreadGroup though.

> > Thanks
>
> Sanjay
puzzlecracker - 29 Jan 2006 17:39 GMT
> I guess if you dont have a contention then dont use Hashtable as it
> would slower the execution.

Hmm. What is the difference in implementation of these datastructures?

Isn't HashMap uses some sort  Black tree and those  get time isO( Log
n)? Whereas in hashtable it is O(1)?

> You can always do this programmatically while spawning threads or use
> ThreadGroup
a. Thread groups are deprecated and not recommended for use.
b. In my case,  I only have access to spawned thread and I'd like to
know what other threads exist, that should include the main thread as
well. I have a feeling that it might be a security violation.

Thanks a lot.
Thomas Hawtin - 29 Jan 2006 18:03 GMT
>> I guess if you dont have a contention then dont use Hashtable as it
>> would slower the execution.
>
> Hmm. What is the difference in implementation of these datastructures?

Almost nothing.

> Isn't HashMap uses some sort  Black tree and those  get time isO( Log
> n)? Whereas in hashtable it is O(1)?

No HashMap is a hash table/map. TreeMap is a red-black tree
implementation. If you stick to declaring references as Map, you can
switch between the different implementations as you like.

HashMap is approximately O(1) in the normal case. However in the worst
case it is O(n).

>> You can always do this programmatically while spawning threads or use
>> ThreadGroup
> a. Thread groups are deprecated and not recommended for use.

ThreadGroup is not deprecated. It's generally not recommended, because
it isn't recommended to do that sort of thing.

> b. In my case,  I only have access to spawned thread and I'd like to
> know what other threads exist, that should include the main thread as
> well. I have a feeling that it might be a security violation.

You should be alright if a SecurityManager is not set. If it does throw,
then that's because you're not allowed at that information, rather than
the particular approach you take.

If it's just for debugging, ThreadGroup should be fine, as is ctrl-\
(ctrl-break in Windows, IIRC) from the console, jdb or other debugger,
or the JVM JMX (java.lang.management) possibly remotely through the
likes of jstack (not Windows) and jconsole.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

zero - 29 Jan 2006 18:17 GMT
> A. what is the difference between Hashtable and HashMap and when would
> you use in instead of the other. It seems to me that HashMap should
> never be used for it provides same functionality as hashtable but
> slower.

I don't know where you got the idea that HashMap is slower than Hashtable.  
HashMap provides constant time operations for simple operations (put, get)
- that's the whole point of hashing (if it's a good hashing algorithm).

The main differences are that HashMap is not synchronized, and permits null
values.  The synchronisation theoretically makes Hashtable slower.
puzzlecracker - 29 Jan 2006 18:47 GMT
> > A. what is the difference between Hashtable and HashMap and when would
> > you use in instead of the other. It seems to me that HashMap should
[quoted text clipped - 7 lines]
> The main differences are that HashMap is not synchronized, and permits null
> values.  The synchronisation theoretically makes Hashtable slower.

Yes, I made a mistake,  I meant TreeMap. When would you use TreeMap is
opposed to HashMap?

Thanks.
zero - 29 Jan 2006 18:57 GMT
> Yes, I made a mistake,  I meant TreeMap. When would you use TreeMap is
> opposed to HashMap?
>
> Thanks.

When you want the keys to be sorted.  Sorting is done with the Comparable
interface, or a Comparator object.  Yes this is slower, but it can be
faster than having to sort them each time you need a sorted view.
puzzlecracker - 29 Jan 2006 19:06 GMT
> > Yes, I made a mistake,  I meant TreeMap. When would you use TreeMap is
> > opposed to HashMap?
[quoted text clipped - 4 lines]
> interface, or a Comparator object.  Yes this is slower, but it can be
> faster than having to sort them each time you need a sorted view.

That does it - just like in cpp.   Thanks.
puzzlecracker - 29 Jan 2006 19:06 GMT
> > Yes, I made a mistake,  I meant TreeMap. When would you use TreeMap is
> > opposed to HashMap?
[quoted text clipped - 4 lines]
> interface, or a Comparator object.  Yes this is slower, but it can be
> faster than having to sort them each time you need a sorted view.

That does it - just like in cpp.   Thanks.
Roedy Green - 30 Jan 2006 04:15 GMT
>I don't know where you got the idea that HashMap is slower than Hashtable.  
>HashMap provides constant time operations for simple operations (put, get)
>- that's the whole point of hashing (if it's a good hashing algorithm).

HashMap is slightly faster since it is does not have the thread-safety
overhead.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Venky - 30 Jan 2006 04:50 GMT
Nice link, gives you most of the information you need about hasmap,
hashtable, treemap..

http://www.javaranch.com/newsletter/Aug2002/mapinterface.html


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.