wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Another Thread Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another Thread Question" Watch "Another Thread Question" New topic
Author

Another Thread Question

sudha javvadi
Ranch Hand

Joined: Mar 23, 2007
Posts: 97
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

Joined: Oct 14, 2002
Posts: 8815
    
    5
please tell us where you got this question!

It helps out the author of the question, and it might help us help you!


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
sudha javvadi
Ranch Hand

Joined: Mar 23, 2007
Posts: 97
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

Joined: Oct 20, 2007
Posts: 513
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 ]

SCJP 5.0
sudha javvadi
Ranch Hand

Joined: Mar 23, 2007
Posts: 97
That's a very nice explanation

thanks Kelvin.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
No problem. Good luck for the exam on Saturday!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Another Thread Question