File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Multiple Threads and single target.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Multiple Threads and single target.." Watch "Multiple Threads and single target.." New topic
Author

Multiple Threads and single target..

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

How to predict the output for the below question? I got it from Master Exam.. Please help!


The answers:
1) It'll keep on printing values which show x and y always as equal and increasing by 1 at each line.
2) It'll keep on printing values which show x and y always as different.
3) Nothing can be said about the sequence of values.
4) It'll keep on printing values which show x and y always as equal and increasing but may increase more than 1 at each line.


I can tell that both values will in a increasing order, but, will they(x and y) be equal? In that question both increments(x++; y++;) in the same line.
And when I run it, both gave same value (when the incrementation is in the same line). But, If I insert those Line 01 and Line 02, They(x and y) differs.

How to approach these kind of question?

Thanks in Advanced!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3202
    
  49

Despite x++; and y++; were in the same line or not, both of those statements are subjected to simultaneous access by two threads. Thus we can NOT say that it should show x and y always as equal.

Devaka


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

So the answer is : Nothing can be said about the sequence of values? But we can sure that those value will be in a incrementing manner.

Thanks in Advanced!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple Threads and single target..