• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Runnable object!?

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I have extracted one of the practice question from K&B 1.4

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

The correct answer is e, but the book says so because the wait is not in a synchronized block, but I thought line 4 would have caused a compile time error already because there is no such a thing as runnable object???

Can anyone clarify?

Thx

Kevin
[ October 30, 2005: Message edited by: Barry Gaunt ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The correct answer is e, but the book says so because the wait is not in a synchronized block, but I thought line 4 would have caused a compile time error already because there is no such a thing as runnable object???

Can anyone clarify?



Line 4 creates an anonymous object. It is not trying to "new" a Runnable, which is an interface. It is creating an object that implements the Runnable interface. The code for this object follows in the next few lines.

Henry
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Henry wrote, that code is sort of like creating an instance of:



That instance is what gets passed to the thread constructor as the job to run when the thread is started.

At line 9 it's like creating an instance of:


Weird but true.

[ October 30, 2005: Message edited by: Barry Gaunt ]
[ October 30, 2005: Message edited by: Barry Gaunt ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic