Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

join() vs. synchronized

 
Kee Kee moon
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below program is from Cathy and Bert.

It will produce the same result, account will not be overdrawn, by using either join() or synchronized. Which one is better, join() or synchronized and why.
Thank you very much in advance.

 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kee Kee moon wrote:
It will produce the same result, account will not be overdrawn, by using either join() or synchronized. Which one is better, join() or synchronized and why.
Thank you very much in advance.


These are two differnt mechanism, that are only somewhat related. Synchronization is a locking mechanism that allows to threads to not step on each other. The join() method call allows one thread to wait for the completion of another thread. In fact, the join() method actually uses synchronization under the covers.

Henry
 
Kee Kee moon
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Kee Kee moon wrote:
It will produce the same result, account will not be overdrawn, by using either join() or synchronized. Which one is better, join() or synchronized and why.
Thank you very much in advance.


These are two differnt mechanism, that are only somewhat related. Synchronization is a locking mechanism that allows to threads to not step on each other. The join() method call allows one thread to wait for the completion of another thread. In fact, the join() method actually uses synchronization under the covers.

Henry


What if I use both, join() and synchronized, any negative impact of using both? The result will be the same if I use both.
Thanks
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kee Kee moon wrote:What if I use both, join() and synchronized, any negative impact of using both? The result will be the same if I use both.

The example code that you have shown here actually doesn't use the join method to provide thread safety. So if you remove synchronization, the account might be overdrawn. Generally synchronization is used to achieve thread safety...
 
Kee Kee moon
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:
Kee Kee moon wrote:What if I use both, join() and synchronized, any negative impact of using both? The result will be the same if I use both.

The example code that you have shown here actually doesn't use the join method to provide thread safety. So if you remove synchronization, the account might be overdrawn. Generally synchronization is used to achieve thread safety...


I got it, thank you very much for the clarification.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic