Few java thread and collection questoins

Discussion in 'Java' started by puzzlecracker, Jan 29, 2006.

  1. 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
     
    puzzlecracker, Jan 29, 2006
    #1
    1. Advertising

  2. puzzlecracker

    Sanjay Guest

    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, Jan 29, 2006
    #2
    1. Advertising

  3. puzzlecracker

    Sanjay Guest

    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
     
    Sanjay, Jan 29, 2006
    #3
  4. 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.
     
    puzzlecracker, Jan 29, 2006
    #4
  5. 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/
     
    Thomas Hawtin, Jan 29, 2006
    #5
  6. puzzlecracker

    zero Guest

    "puzzlecracker" <> wrote in
    news::

    > 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.
     
    zero, Jan 29, 2006
    #6
  7. Re: Few java thread and collection questoins

    zero wrote:
    > "puzzlecracker" <> wrote in
    > news::
    >
    > > 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.
     
    puzzlecracker, Jan 29, 2006
    #7
  8. puzzlecracker

    zero Guest

    Re: Few java thread and collection questoins

    "puzzlecracker" <> wrote in
    news::

    >
    >
    > 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.
     
    zero, Jan 29, 2006
    #8
  9. Re: Few java thread and collection questoins

    zero wrote:
    > "puzzlecracker" <> wrote in
    > news::
    >
    > >
    > >
    > > 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, Jan 29, 2006
    #9
  10. Re: Few java thread and collection questoins

    zero wrote:
    > "puzzlecracker" <> wrote in
    > news::
    >
    > >
    > >
    > > 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, Jan 29, 2006
    #10
  11. puzzlecracker

    Roedy Green Guest

    On Sun, 29 Jan 2006 18:17:51 GMT, zero <> wrote, quoted or
    indirectly quoted someone who said :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Jan 30, 2006
    #11
  12. puzzlecracker

    Venky Guest

    Venky, Jan 30, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dylan
    Replies:
    5
    Views:
    444
    Daniel T.
    Mar 22, 2005
  2. Pradeep
    Replies:
    2
    Views:
    695
    Patricia Shanahan
    Jan 24, 2007
  3. Murali
    Replies:
    2
    Views:
    609
    Jerry Coffin
    Mar 9, 2006
  4. Øyvind Isaksen
    Replies:
    1
    Views:
    1,024
    Øyvind Isaksen
    May 18, 2007
  5. Hemant

    create collection of collection

    Hemant, Oct 22, 2009, in forum: ASP .Net
    Replies:
    1
    Views:
    435
    Gregory A. Beamer
    Oct 22, 2009
Loading...

Share This Page