| Author |
join() vs. synchronized
|
Kee Kee moon
Ranch Hand
Joined: Dec 11, 2009
Posts: 140
|
|
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: 16811
|
|
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: 140
|
|
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
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
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: 140
|
|
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.
|
 |
 |
|
|
subject: join() vs. synchronized
|
|
|