• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

join() vs. synchronized

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic