Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads question.

 
Alan Blass
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • 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.
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4502
268
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan, not everyone has that book. Please post the entire question and ShowSomeEffort
 
Rohini Shirpewar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
fadi aboona
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • 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,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic