This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes join() vs. synchronized Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "join() vs. synchronized" Watch "join() vs. synchronized" New topic
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
    
  19

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
    
    2

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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: join() vs. synchronized
 
Similar Threads
Synchronized method won't let other thread go, why?
Regarding Syncronization
Synchronised Vs. Non synchronised Thread
queries on threads
Problem with threads (concurrency)