This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sun Practice Exam - Concurrency > Objective 4.3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Sun Practice Exam - Concurrency > Objective 4.3 " Watch "Sun Practice Exam - Concurrency > Objective 4.3 " New topic
Author

Sun Practice Exam - Concurrency > Objective 4.3

Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

How should we approach such questions? I am not able to understand what is going on in this program


5. class NoGo implements Runnable {
6. private int i;
7. public void run() {
8. if (i%10 != 0) { i++; }
9. for(int x=0; x<10; x++, i++)
10. { if (x == 4) Thread.yield(); }
11. System.out.print(i + " ");
12. }
13. public static void main(String [] args) {
14. NoGo n = new NoGo();
15. for(int x=0; x<100; x++) { new Thread(n).start(); }
16. }
17. }


Which is true?


A
The output can never contain the value 10.
B
The output can never contain the value 30.
C
The output can never contain the value 297.
D
The output can never contain the value 1020.
E
The output can never contain the value 1130.


===Vyas Sanzgiri===
My Blog
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Well my logic says that only option E is correct. And that's because there are 100 threads and each can increment the value of i by max 11. So 100 * 11 = 1100 and thus the output can never be 1130. Other values look feasible to me as there are so many threads and yeilds involved that any value out of those can be displayed...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sun Practice Exam - Concurrency > Objective 4.3
 
Similar Threads
Thread doubt
Unsure about thread algorithm
problem in threads(sun accesibility test)
Threads - How to approach such a question?
thread question from sun's Free Proficiency Assessment System