• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Threads and single target..

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4329
238
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic