• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
  • Himai Minh
  • Carey Brown
  • salvin francis

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

Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.................
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
[ November 27, 2007: Message edited by: Deepak Jain ]
All that thinking. Doesn't it hurt? What do you think about this tiny ad?
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic