Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

When to use "thread-safe" classes?

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In SCJP p 716 it's mentioned:

"The moral here is that just because a class is described as "thread-safe" doesn't mean it is always thread-safe."

and

"...Collections.synchronizedList() may well become redundant.

Being the authors are synchronizing their own methods here, and recommending that we do so, I'm wondering when/where/why we need the synchronized classes. (being they say the synchronization is redundant) I'm not very experienced with concurrency, so if someone can explain when we would use the java synchronized classes I'd appreciate that. What is an example of using these sync classes, where we can rely on their methods being synchronized, and not have to do additional sychronization?
 
Dave Reinhardt
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone help me understand this concept?
 
Ranch Hand
Posts: 463
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Practically speaking, as mentioned in the K&B book, there is NO synchronized class. There ARE synchronized methods. If class A has methods AMethod1() and AMethod2(), if we synchronize both these methods, we can say A is synchronized.

The synchronized keyword cannot be applied to classes.
Hope this helps. Correct me if I am wrong.

- surya.
 
Sai Surya
Ranch Hand
Posts: 463
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dave Reinhardt:

What is an example of using these sync classes, where we can rely on their methods being synchronized, and not have to do additional sychronization?



In multithreaded environment we need to use synchronized methods. More precisely,

class BankAccount {
double balance;

BankAccount(double initialbalance) {
balance = initialbalance;
}

public void synchronized depositCash(double amount) {
balance = balance + amount;
}

public double synchronized getCash(double amount) {
balance = balance - amount;
return amount;
}

}

In the above case, the following happens if methods are synchronizing..

1. You initialized the account with 100$
2. I deposited amount 50$, so total is 150$
3. Somebody withdraws 100$, it will be 50$.

If synchrnozation (methods not synchronized) missing then,

1. You initialized the account with 100$
2. I deposited amount 50$, so total is 150$
3. Some body withdraws total money, leaving 0 balance.
4. But some old thread still has old variable state of balance with 100$.
If that thread gets a call to withdraw the cash, then there is no balance at all in reality, but thread has a balance of 100 so we will go into -100$.

I hope you understood.
correct me if i am wrong.

surya.
[ August 25, 2006: Message edited by: Sai Surya ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic