This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 2885
    
  13

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
My Home Page -- Twitter Profile -- JavaRanch FAQ -- 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..
 
Similar Threads
JQ+ Mock - Threads - Question ID: 988384270281
Synchronization practice: why does thread1 rush through loop without pause or reset?
join() method in Thread doesn't work as expected
JQ plus thread question