Single Threaded vs Thread Safe

What is the difference between Single Threaded Class/Object and Thread Safe Class/Object. If would be great if someone explain the difference with some simple example.
[181 byte] By [chandu_jja] at [2007-11-26 19:10:25]
# 1
An object which isn't designed to be used by multiple threads concurrently might produce incorrect results.Kaj
kajbja at 2007-7-9 21:06:22 > top of Java-index,Java Essentials,Java Programming...
# 2
Imagine if you have two threads both having a reference to an object. The first thread might call a set method but it has no guarantee that when it calls the get the same value will be returned, the second thread may have updated the object in the meantime.Ted.
ted_trippina at 2007-7-9 21:06:22 > top of Java-index,Java Essentials,Java Programming...
# 3
when you say this method is thread safe you guarantee there is only one thread access this method at atime
eaajea at 2007-7-9 21:06:22 > top of Java-index,Java Essentials,Java Programming...
# 4

> when you say this method is thread safe

>

> you guarantee there is only one thread access this

> method at atime

Actually the truth is nearly the opposite. When you say some piece of code is thread safe, you are saying it's written in such a way that it's safe for serveral threads to be running it at the same time. That may involve protecting parts of the code with synchronized, but it's mostly about being careful with common state data.

It's when code isn't thread safe you may need to synchronize it.

malcolmmca at 2007-7-9 21:06:22 > top of Java-index,Java Essentials,Java Programming...
# 5

> > when you say this method is thread safe

> >

> > you guarantee there is only one thread access

> this

> > method at atime

>

> Actually the truth is nearly the opposite. When you

> say some piece of code is thread safe, you are saying

> it's written in such a way that it's safe for

> serveral threads to be running it at the same time.

> That may involve protecting parts of the code with

> synchronized, but it's mostly about being careful

> with common state data.

>

> It's when code isn't thread safe you may need to

> synchronize it.

ooh sorry thanks malc u r right

eaajea at 2007-7-9 21:06:22 > top of Java-index,Java Essentials,Java Programming...
# 6
HiThanks for good responses, but all are talking about Thread Safe Classes.Single Threaded Class == Thread Safe Class ?thanx
chandu_jja at 2007-7-9 21:06:22 > top of Java-index,Java Essentials,Java Programming...
# 7
Single Threaded Class != Thread Safe Class
DrLaszloJamfa at 2007-7-9 21:06:22 > top of Java-index,Java Essentials,Java Programming...
# 8
> Single Threaded Class == Thread Safe Class ?No! It's not because you write a class that doesn't use multiple threads, that your class cannot be used by multiple threads.
Peetzorea at 2007-7-9 21:06:22 > top of Java-index,Java Essentials,Java Programming...
# 9

Can you explain what you mean by "single threaded object"?

I have never heard of such a thing. You can have an application with one thread or multiple threads and an object may be referenced by multiple threads. Do you mean an object that is only allowed to exist on a single thread? Are you talking about ThreadLocal objects?

Ted.

ted_trippina at 2007-7-9 21:06:22 > top of Java-index,Java Essentials,Java Programming...
# 10

How do I make a single threaded ?

To make Thread safe we use synchronized keyword

I need more clarity on this

It will be great if somebody can explain with a very simple example

DrLaszloJamf & Peetzore, I think you people can give me more detailed answer.

thnx

chandu_jja at 2007-7-9 21:06:22 > top of Java-index,Java Essentials,Java Programming...
# 11
http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html
DrLaszloJamfa at 2007-7-9 21:06:22 > top of Java-index,Java Essentials,Java Programming...
# 12

> How do I make a single threaded ?

> To make Thread safe we use synchronized

> keyword

>

> I need more clarity on this

>

You sir are a complete bafoon.

Do you is needing more clarify on this? I'll put some text in bold too for no reason.

Ted.

ted_trippina at 2007-7-9 21:06:22 > top of Java-index,Java Essentials,Java Programming...