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 / March 2005

Tip: Looking for answers? Try searching our database.

Is Hashtable synchronised?

Thread view: 
JamesY - 07 Mar 2005 04:34 GMT
Hi,

From the doc for  java.util.Hashtable:
As of the Java 2 platform v1.2, this class has been retrofitted to implement
Map, so that it becomes a part of Java's collection framework. Unlike the
new collection implementations, Hashtable is synchronized.

So is Hashtable synchronized?

Regards,
James
Dotty - 07 Mar 2005 05:11 GMT
> Hi,
>
[quoted text clipped - 7 lines]
> Regards,
> James

It has synchronized methods, which make it thread safe.

Let me guess your next question "is it thread safe?"
Aquila Deus - 07 Mar 2005 06:05 GMT
> > Hi,
> >
[quoted text clipped - 12 lines]
>
> Let me guess your next question "is it thread safe?"

Not if he implements coroutines in Java :)
John C. Bollinger - 07 Mar 2005 14:56 GMT
>>So is Hashtable synchronized?

> It has synchronized methods, which make it thread safe.

Yes, it has synchronized methods.  No, that does not make it thread-safe.

Signature

John Bollinger
jobollin@indiana.edu

Dotty - 07 Mar 2005 19:09 GMT
> >>So is Hashtable synchronized?
>
> > It has synchronized methods, which make it thread safe.
>
> Yes, it has synchronized methods.  No, that does not make it thread-safe.

This confuses me. My book says it is thread-safe.
If it is not thread-safe, then what does 'synchronized' do?
Oscar kind - 07 Mar 2005 20:39 GMT
>> >>So is Hashtable synchronized?
>>
[quoted text clipped - 4 lines]
> This confuses me. My book says it is thread-safe.
> If it is not thread-safe, then what does 'synchronized' do?

Using the synchronized keyword doesn't make a code block or method
thread-safe. It's just one of the tools to make it thread-safe. For more
information, see Roedy Green's entry in his Java Glossery:

   http://www.mindprod.com/jgloss/threadsafe.html

Basically, with both the synchronized keyword and a good design, you can
make a class thread-safe. And even here the wording is a trap: the design
is by far the most important part in making a class thread-safe. This is
also shown in the example at the above URL.

Signature

Oscar Kind                                    http://home.hccnet.nl/okind/
Software Developer                    for contact information, see website

PGP Key fingerprint:    91F3 6C72 F465 5E98 C246  61D9 2C32 8E24 097B B4E2

Dotty - 07 Mar 2005 22:02 GMT
> > This confuses me. My book says it is thread-safe.
> > If it is not thread-safe, then what does 'synchronized' do?
[quoted text clipped - 4 lines]
>
>     http://www.mindprod.com/jgloss/threadsafe.html

So you are saying that Dave Flanagan is wrong when he says
it is thread-safe. Maybe I need to go back to bed.
Andrew McDonagh - 07 Mar 2005 23:24 GMT
>>>This confuses me. My book says it is thread-safe.
>>>If it is not thread-safe, then what does 'synchronized' do?
[quoted text clipped - 7 lines]
> So you are saying that Dave Flanagan is wrong when he says
> it is thread-safe. Maybe I need to go back to bed.

Not sure if he's mistaken or just used unfortunate wording.

Essentially, having a bunch of synchronized methods on a class, means
that for the duration of the client code calling those individual
methods, there is a lock preventing another thread accessing something.

However, if the client code does not obtain a lock on the entire object
(or the part of it that needs protecting) when calling those
synchronized  methods, then multiple threads can cause problems.

E.g.

class MySynchronizedClass {
  public synchronized int getValue() {
    return theValue;
  }

  public synchronized void increment() {
     theValue++;
  }

  private int theValue = 0;
}

Now, the two methods are synchronized, but unless the client code which
makes use of both methods, synchronizes the entire object, then we have
problems. E.g.

// client code using the above class.

void someMethod(MySynchronizedClass synchronizedClass) {
   if (synchronizedClass.getValue() == 10) {
      synchronizedClass.increment();
   }
}

The problem here, is that the lock is obtained for the duration of each
method. So lock is obtained when getValue() is called, then released.
Then obtained again when increment() is called and released.

This means that during the time it takes one thread to move from the
getValue() to the increment() call, another thread may have already
called increment().  Which would mean that the increment() method has
been called twice.

The code should be...

void someMethod(MySynchronizedClass synchronizedClass) {

   synchronize(synchronizedClass) {
     if (synchronizedClass.getValue() == 10) {
        synchronizedClass.increment();
     }
   }

}

This mean the lock will be obtained for the entire duration and no other
thread can get between the two methods.
Lasse Reichstein Nielsen - 08 Mar 2005 00:52 GMT
>> So you are saying that Dave Flanagan is wrong when he says
>> it is thread-safe. Maybe I need to go back to bed.
>
> Not sure if he's mistaken or just used unfortunate wording.

It could very well be a matter of definition.

When I hear that a class is thread safe, I only assume that calling
methods on an object of that class from different threads will not
leave the object in an inconsistent configuration. With that as a
defintion, Hashtable is thread safe, as are your examples.
We can call that "internally thread safe".

If I understand you correctly, you also require that a thread safe
object must maintain a sort of logical consistency, where not only
is the internal state consistent, it is also correct.

I would call that thread safe programming, which can be bloddy hard
in practice, exactly because it can't be handled by simply making
an object (internally) thread safe.

/L
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

Oscar kind - 07 Mar 2005 23:42 GMT
>> > This confuses me. My book says it is thread-safe.
>> > If it is not thread-safe, then what does 'synchronized' do?
[quoted text clipped - 7 lines]
> So you are saying that Dave Flanagan is wrong when he says
> it is thread-safe. Maybe I need to go back to bed.

Is he wrong? Or does he make the additional assumption that the class is
well designed? My opinion of the standard Java classes is that they are,
although arguably not perfect.

Signature

Oscar Kind                                    http://home.hccnet.nl/okind/
Software Developer                    for contact information, see website

PGP Key fingerprint:    91F3 6C72 F465 5E98 C246  61D9 2C32 8E24 097B B4E2

Dotty - 08 Mar 2005 00:23 GMT
> >> > This confuses me. My book says it is thread-safe.
> >> > If it is not thread-safe, then what does 'synchronized' do?
[quoted text clipped - 11 lines]
> well designed? My opinion of the standard Java classes is that they are,
> although arguably not perfect.

So I guess what everyone is saying is that no matter what SUN says or no
matter what the books say, there is no such thing as a thread save thing of
any kind unless you write 100% of the code yourself and you don't screw it
up.
Andrew McDonagh - 08 Mar 2005 00:47 GMT
>>>>>This confuses me. My book says it is thread-safe.
>>>>>If it is not thread-safe, then what does 'synchronized' do?
[quoted text clipped - 19 lines]
> any kind unless you write 100% of the code yourself and you don't screw it
> up.

Kind of, what Sun provides are classes/libraries that make use of
various thread safety enabling features (synchronized keyword usage,
Wait & Notifys, etc) and with the latest version of java a whole suite
of thread utilities.

However, they are all tools for us to use to create thread safe code.
Scott Ellsworth - 07 Mar 2005 21:04 GMT
> > >>So is Hashtable synchronized?
> >
[quoted text clipped - 4 lines]
> This confuses me. My book says it is thread-safe.
> If it is not thread-safe, then what does 'synchronized' do?

It means that access to those specific methods is synchronized.  
Unfortunately, it is rare to just want a single get method call to be
synched - you usually want to to a get-update or get-insert or
get-delete call, and the table can change between any of these methods.

This is why most books advise putting synchronized blocks around the
entire block of code that needs to be locked, rather than using synched
methods in the Hashtable/Vector classes.   If you are going to do that,
of course, you usually want to use an unsynchronized collection, as you
are managing access yourself.

Scott
Chris Smith - 07 Mar 2005 05:20 GMT
> From the doc for  java.util.Hashtable:
> As of the Java 2 platform v1.2, this class has been retrofitted to implement
> Map, so that it becomes a part of Java's collection framework. Unlike the
> new collection implementations, Hashtable is synchronized.
>
> So is Hashtable synchronized?

Yes.  You just finished quoting the section of the docs saying that it
is synchronized.  Do you think they are lying to you?

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

JamesY - 07 Mar 2005 16:27 GMT
> > From the doc for  java.util.Hashtable:
> > As of the Java 2 platform v1.2, this class has been retrofitted to implement
[quoted text clipped - 5 lines]
> Yes.  You just finished quoting the section of the docs saying that it
> is synchronized.  Do you think they are lying to you?

Well, first, it mentioned that Hashtable beomes part of collection
framework.
Then it says that unlike the new collection implementation, Hashtable is
synchronized.

The 2nd sentence seemed confusing.
Does it means that after Hashtable implements Map (which is to be the new
collection framework), the new Hashtable (that implemented the Map) isn't
synchronised anymore?

Thanks all for the clarification ;-)
JamesY - 07 Mar 2005 16:52 GMT
> > > From the doc for  java.util.Hashtable:
> > > As of the Java 2 platform v1.2, this class has been retrofitted to
[quoted text clipped - 19 lines]
>
> Thanks all for the clarification ;-)

Maybe the doc will be clearer if it uses the word "other" as below:

As of the Java 2 platform v1.2, this class has been retrofitted to implement
Map, so that it becomes a part of Java's collection framework. Unlike other
new collection implementations, Hashtable is synchronized.
Darryl Pierce - 07 Mar 2005 12:52 GMT
> From the doc for  java.util.Hashtable:
> As of the Java 2 platform v1.2, this class has been retrofitted to implement
> Map, so that it becomes a part of Java's collection framework. Unlike the
> new collection implementations, Hashtable is synchronized.
>
> So is Hashtable synchronized?

No, it's not.

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Visit my homepage: http://mcpierce.multiply.com
"By doubting we come to inquiry, through inquiry truth." - Peter Abelard

Darryl Pierce - 07 Mar 2005 12:53 GMT
> From the doc for  java.util.Hashtable:
> As of the Java 2 platform v1.2, this class has been retrofitted to implement
> Map, so that it becomes a part of Java's collection framework. Unlike the
> new collection implementations, Hashtable is synchronized.
>
> So is Hashtable synchronized?

Sorry, brain fart on my part. Yes, it's synchronized. I was thinking
HashMap...

Signature

Darryl L. Pierce <mcpierce@gmail.com>
Visit my homepage: http://mcpierce.multiply.com
"By doubting we come to inquiry, through inquiry truth." - Peter Abelard



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



©2008 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.