wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes book K&B self test q16........problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "book K&B self test q16........problem" Watch "book K&B self test q16........problem" New topic
Author

book K&B self test q16........problem

sapana jain
Ranch Hand

Joined: Oct 24, 2007
Posts: 42
Given the following,
1. public class Test {
2. public static void main (String [] args) {
3. final Foo f = new Foo();
4. Thread t = new Thread(new Runnable() {
5. public void run() {
6. f.doStuff();
7. }
8. });
9. Thread g = new Thread() {
10. public void run() {
11. f.doStuff();
12. }
13. };
14. t.start();
15. g.start();
16. }
17. }
1. class Foo {
2. int x = 5;
3. public void doStuff() {
4. if (x < 10) {
5. // nothing to do
6. try {
7. wait();
8. } catch(InterruptedException ex) { }
9. } else {
10. System.out.println("x is " + x++);
11. if (x >= 10) {
12. notify();
13. }
14. }
15. }
16. }
What is the result?
A. The code will not compile because of an error on line 12 of class Foo.
B. The code will not compile because of an error on line 7 of class Foo.
C. The code will not compile because of an error on line 4 of class Test.
D. The code will not compile because of some other error in class Test.
E. An exception occurs at runtime.
F. x is 5
x is 6

when i am running the code with modifying the code with synchronization with doStuff method..............its not showing the output as they have explain in answers please help me.................
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Hi Sapana,



Here is the proper code that works.
There are multiple issues with the code.
1. Rememeber making a class thread safe means the code must execute across threads properly.
2. wait() and notify() when invoked directly they get invoked using "this" currently executing object. So when you say wait() its like this.wait() and now the current thread will wait using the this lock object and when someone invokes notify it must be invoked using the same lock object. Since the lock object here is this , it must be invoked as this.notify() or notify(). when you invoke notify(), it will wake up all the threads that are waiting on "this" object.
3. Remember in the above code the new Foo object is final. which means the two threads that are created [1st using Runnable interface] and [2nd extending Thread class] Both of them invoke doStuff() using the same f instance. If you create two different f instance then both the threads will wait forever.
4. There is a flaw in the code. With the above changes both the threads will wait for ever because x will not have a proper value. Hence i modified the code to increment x by 5.

You can even modify the code that uses a static lock object. In that case remember to use the static lock object in synchronized block and invoke wait() and notify() with the same static lock object.
Hope this helps.
Thanks
Deepak
[ November 27, 2007: Message edited by: Deepak Jain ]

SCJP, SCWCD, SCBCD
 
Don't get me started about those stupid light bulbs.
 
subject: book K&B self test q16........problem
 
Similar Threads
wait() and notify() question...
Runnable object!?
using wait() method in Threads
Question on thread q.15 pg 754
Question on therad q.16 pg755