Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Few java thread and collection questoins (http://www.velocityreviews.com/forums/t149765-few-java-thread-and-collection-questoins.html)

puzzlecracker 01-29-2006 05:07 PM

Few java thread and collection questoins
 
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 01-29-2006 05:28 PM

Re: Few java thread and collection questoins
 
puzzlecracker wrote:
> 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 01-29-2006 05:36 PM

Re: Few java thread and collection questoins
 

Sanjay wrote:
> puzzlecracker wrote:
> > 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


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

>
> >
> >
> > Thanks

>
> Sanjay



puzzlecracker 01-29-2006 05:39 PM

Re: Few java thread and collection questoins
 

> 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 01-29-2006 06:17 PM

Re: Few java thread and collection questoins
 
puzzlecracker wrote:
>> 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
--
Unemployed English Java programmer
http://jroller.com/page/tackline/

zero 01-29-2006 06:17 PM

Re: Few java thread and collection questoins
 
"puzzlecracker" <ironsel2000@gmail.com> wrote in
news:1138554450.184665.83290@g43g2000cwa.googlegro ups.com:

> 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 01-29-2006 06:47 PM

Re: Few java thread and collection questoins
 

zero wrote:
> "puzzlecracker" <ironsel2000@gmail.com> wrote in
> news:1138554450.184665.83290@g43g2000cwa.googlegro ups.com:
>
> > 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.



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

Thanks.


zero 01-29-2006 06:57 PM

Re: Few java thread and collection questoins
 
"puzzlecracker" <ironsel2000@gmail.com> wrote in
news:1138560453.741165.222770@f14g2000cwb.googlegr oups.com:

>
>
> 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 01-29-2006 07:06 PM

Re: Few java thread and collection questoins
 

zero wrote:
> "puzzlecracker" <ironsel2000@gmail.com> wrote in
> news:1138560453.741165.222770@f14g2000cwb.googlegr oups.com:
>
> >
> >
> > 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.


That does it - just like in cpp. Thanks.


puzzlecracker 01-29-2006 07:06 PM

Re: Few java thread and collection questoins
 

zero wrote:
> "puzzlecracker" <ironsel2000@gmail.com> wrote in
> news:1138560453.741165.222770@f14g2000cwb.googlegr oups.com:
>
> >
> >
> > 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.


That does it - just like in cpp. Thanks.



All times are GMT. The time now is 12:14 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.