aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes join() vs. synchronized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "join() vs. synchronized" Watch "join() vs. synchronized" New topic
Author

join() vs. synchronized

Kee Kee moon
Ranch Hand

Joined: Dec 11, 2009
Posts: 147
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
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kee Kee moon
Ranch Hand

Joined: Dec 11, 2009
Posts: 147
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

Joined: Aug 03, 2008
Posts: 9293
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Kee Kee moon
Ranch Hand

Joined: Dec 11, 2009
Posts: 147
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: join() vs. synchronized