• 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

Threads question.

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I am studying Bates and Sierra's book page 770 question 16.

The answer says "c has not been assigned to an object". But isn't c assigned to new Chicks() on line 17?

Please advice. Thanks.
 
Sheriff
Posts: 7135
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alan, not everyone has that book. Please post the entire question and ShowSomeEffort
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here is the complete question.




Which are true? (Choose all that apply.)
A. Compilation fails
B. The output could be 4 4 2 3
C. The output could be 4 4 2 2
D. The output could be 4 4 4 2
E. The output could be 2 2 4 4
F. An exception is thrown at runtime


The c at line 17 is assigned, however it belongs to the Thread main. Notice that, two new Threads are provided with new ChicksYack(), they have their own c, which has not been initializes anywhere.

HTS.

Rohini.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohini Shirpewar wrote:

The c at line 17 is assigned, however it belongs to the Thread main.


Nope, it's belong to an instance of ChicksYack.

Rohini Shirpewar wrote:
which has not been initializes anywhere.


Then, what about the go method? what's happening inside that?
 
Rohini Shirpewar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Abimaran,

Yes you are correct the c at line 17 belongs to the instance of ChicksYack.

However, at line 18 and 19 the new Thread is provided with new ChicksYack, which have their own c and those are not assigned anywhere.
Right? or my understanding isn't correct here ?

Rohini.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohini Shirpewar wrote:
However, at line 18 and 19 the new Thread is provided with new ChicksYack, which have their own c and those are not assigned anywhere.
Right? or my understanding isn't correct here ?

Rohini.



mm, That's correct. You'll get NullPointerException.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:

Rohini Shirpewar wrote:
However, at line 18 and 19 the new Thread is provided with new ChicksYack, which have their own c and those are not assigned anywhere.
Right? or my understanding isn't correct here ?

Rohini.



mm, That's correct. You'll get NullPointerException.



if i provide a constructor for ChicksYack and inside it i assign new Chicks() to c then that would have no effect on the synchronized yack(long id) because now i have two different Chicks instances, please confirm.

Thanks,
 
reply
    Bookmark Topic Watch Topic
  • New Topic