The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread doubt (examlab question) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Thread doubt (examlab question)" Watch "Thread doubt (examlab question)" New topic
Author

Thread doubt (examlab question)

Emanuele Ghe
Ranch Hand

Joined: Feb 04, 2009
Posts: 111
Hi,
this code comes from an exam lab question:


The answer of this question is


In the explanation is stated that, even if the run() method is synchronized, there is no way to know
which thread will run first.

I think this explanation is misleading.
In my opinion the result is unpredictable because, since 2 threads are started with 2 different A objects, the synchronization is useless, because each object will acquire the lock on its A object independently from each other.

What do you think ?

p.s. I don't want to critic devaka work, is awesome. I just wanna be sure to understand everithing in the correct way.

Thanks.

SCJP6 with score 90%. I am conscious of my ignorance and ready to learn from everyone.
Javier Cortes
Greenhorn

Joined: May 26, 2010
Posts: 6
You can indeed drop synchronized on the run method because, as you said, each Thread object is getting different locks when needed. Since synchronization was useless try and create a single object A and send it as target to both rba and rbb (don't synchronize either). You are at the same scenario here, sort of proof by contrapositive. Then, it all boils down to the fact that the JVM doesn't guarantee that the order in which the threads are started is going to be the order in which they'll become runnable.
Emanuele Ghe
Ranch Hand

Joined: Feb 04, 2009
Posts: 111
Javier Cortes wrote:You can indeed drop synchronized on the run method because, as you said, each Thread object is getting different locks when needed. Since synchronization was useless try and create a single object A and send it as target to both rba and rbb (don't synchronize either). You are at the same scenario here, sort of proof by contrapositive. Then, it all boils down to the fact that the JVM doesn't guarantee that the order in which the threads are started is going to be the order in which they'll become runnable.


Thank you, you confirmed my thoughts.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread doubt (examlab question)
 
Similar Threads
Examlab(SCJP 6 Practice exam 3) Thread Question
why Compile time error when trying to us static with Synchronized ?
Doubt on a Devaka Cooray ExamLab practice question
Examlab Thread question
Threads again