• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class is Thread-Safe or not?

 
Surender Singh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
Please let me know below my class code is thread-safe or not? if not,then let me know how to make it thread-safe?.

class Thread_safe {
private String name;
public synchronized void test(){
name="Test";
}
public void impl(){
name="Impl";
}
}
Thanks
Regards
Surender Singh
 
Chris Janicki
Greenhorn
Posts: 23
Mac OS X Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well it is thread-safe, but because is just doesn't do anything!

Classes become unsafe when two different threads can interact with the class at the same time... one changing the classes data while the other is reading. That's when you need synchronization... to protect the class during changes, so that any references to the data are safe from being changed during the referencing.

If your class had the following method, then you would need to synchronize all three methods:

toString()
{
System.out.println("I think my name is " +name);
// this is currently unsafe, since a thread could call test() or impl() in between these two lines,
// which would look very confusing to person reading your output.
System.out.println("Yes, my name is definitely " +name);
}

[ May 05, 2008: Message edited by: Chris Janicki ]
[ May 05, 2008: Message edited by: Chris Janicki ]
 
Surender Singh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chris for reply
My question is like
Suppose Two threads access same instance of class and one thread is invoking Synchronized method ,other thread is invoking the unsynchronized method on same the object then how we make class "Thread-Safe"in that case...
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surender:
Suppose Two threads access same instance of class and one thread is invoking Synchronized method ,other thread is invoking the unsynchronized method on same the object then how we make class "Thread-Safe"in that case...


If the unsynchronized method is changing the shared state of the class then it must be synchronized at the least. There is no other magic that will make the class thread-safe.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Nitesh]: If the unsynchronized method is changing the shared state of the class then it must be synchronized at the least.

I would add, if it's charging the state or if it's reading the state, then it must be synchronized. Generally. Otherwise there is a possibility that some threads will see an older value of the data. Sometimes this may be acceptable however. If there's just one field that changes, and its data type is double or long, then an unsynchronized read may see even stranger results. Like if a number changes from 0 to 0x12345678, it might be seen as 0x12340000 or 0x00005678. So generally you need to synchronized readers as well as writers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!