Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another Thread Question

 
sudha javvadi
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I need answer for the below question.If possible with little explanation.

3. class Waiting2 implements Runnable {
4. int state;
5. public synchronized void run() {
6. if (state++ < 3) {
7. System.out.print(" " + Thread.currentThread().getId());
8. try { this.wait(); } catch (Exception e) { }
9. System.out.print(" " + Thread.currentThread().getId());
10. }
11. else {
12. try { Thread.sleep(2000); } catch (Exception e) { }
13. notify();
14. notifyAll();
15. }
16. }
17. public static void main(String [] args) {
18. Waiting2 w = new Waiting2();
19. new Thread(w).start();
20. new Thread(w).start();
21. new Thread(w).start();
22. new Thread(w).start();
23. }
24. }
Which two results are possible? (Choose two.)

6 7 8 9
6 7 8 6
6 7 8 6 7 8
6 7 8 6 7 9
6 7 8 8 6 7
6 7 8 6 6 7 8
6 7 8 9 6 7 8 9
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please tell us where you got this question!

It helps out the author of the question, and it might help us help you!
 
sudha javvadi
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bert,
It is from http://www.sun.com/training/certification/assessment/055.jsp .
I included the link in my previous post and i posted this immediately. I just finished assessment exam and i got 64%. How much i should score in mock tests to pass the real one. I am gonna take exam on this sat.

Thanks,
sudha
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, each of the first three threads started by this program will acquire a lock on w (the Waiting2 instance), print out its thread ID (some integer value), and then wait for a notification on w (giving up the lock while waiting). The fourth thread also acquires a lock on w, but does not produce any output. Instead, it will send a notification to wake all the previous threads. Each of the previous threads will then re-acquire the lock on w in some unknown order, and print its thread ID again before terminating.

The net result of this is that you should see exactly three different thread IDs in the output, with each ID occurring twice (once among the first three numbers, then again among the last three numbers). The order in which these thread IDs appear is not fixed, since you can't predict the order in which the threads will re-acquire w's lock.

Based on the above analysis, the correct answers are "6 7 8 6 7 8" and "6 7 8 8 6 7". Note that the thread IDs don't necessarily have to appear in ascending order among the first three numbers. So if "8 7 6 7 8 6" were one of the available options, it would have been a valid answer too.
[ October 31, 2007: Message edited by: Kelvin Lim ]
 
sudha javvadi
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a very nice explanation

thanks Kelvin.
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem. Good luck for the exam on Saturday!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic