| Author |
Using non-static object as lock in Threads PRoblem
|
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
KB book self test
Question 16
i ran the above code and got the following error:
Exception in thread "Thread-0" java.lang.NullPointerException
at ChicksYack.run(ChicksYack.java:20)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-1" java.lang.NullPointerException
at ChicksYack.run(ChicksYack.java:20)
at java.lang.Thread.run(Thread.java:619)
Answer as given in the book:
When run() is invoked, it is with a new instance of ChicksYack and c has
not been assigned to an object. so,Runtime Exception
Queries
1.But c has been to an object on line 15,then why is there a Runtime Exception
2.If I change Chicks c to static Chicks c,the code run fines.
can anyone explain ?
I cannot get the answer as given in book
|
OCPJP 6.0 93%
OCPJWCD 5.0 98%
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16681
|
|
mohitkumar gupta wrote:
1.But c has been to an object on line 15,then why is there a Runtime Exception
Yes, but there are three objects -- one at line 12, one at line 16, and one at line 17. Is the assignment of "c" at line 15, the same variable that is used at line 20?
mohitkumar gupta wrote:
2.If I change Chicks c to static Chicks c,the code run fines.
Can you explain the difference between a static and instance variable? and how the previous question may be affected by this?
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
|
This question has already discussed recently.
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
i was not able to find this question in the posts.please refer to that link if anyone finds it.
Chicks c is an instance variable of ChicksYack.
each ChicksYack object will have it's own copy of c.
line 12,16,17 have a new ChicksYack object created.
when the line 16,17 are run,then these threads would start and their run methods would be called.The run() method contains call to yack function through c object.
Since c has been assigned to a Chicks object on line 15.
i think that code would run
but i am getting a runtime error.
is yack function is not been called as c doesnot refers to any object ?
but c is been assigned to object on line 15.
please explain
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16681
|
|
mohitkumar gupta wrote:
Chicks c is an instance variable of ChicksYack.
each ChicksYack object will have it's own copy of c.
line 12,16,17 have a new ChicksYack object created.
when the line 16,17 are run,then these threads would start and their run methods would be called.The run() method contains call to yack function through c object.
Since c has been assigned to a Chicks object on line 15.
i think that code would run
but i am getting a runtime error.
is yack function is not been called as c doesnot refers to any object ?
but c is been assigned to object on line 15.
please explain
As already hinted at, in my last post, the "c" variable that has been assigned at line 15, is not the same "c" variable used by the run() method, by the newly created threads. Work it out on paper. It's straightforward.
Henry
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
mohitkumar gupta wrote:
please explain
This is your code,
Here, ChicksYack is a class, which HAS - A Chick object. So for every instance of the ChicksYack class, there should be a Chick object. In line 12 you create a ChicksYack object, and call go() method on that instance. But the Chick object associted with ChicksYack object is not created yet(simply it's null), and in the go() method, you create a Chick object and assign that object to your already created ChicksYack object's member field c(to the field variable c). And, in the go() method, you create another two ChicksYack objects(in lines 16,17), to pass them to Threads.
And if I ask you, do those object, which you created in the lines 16,17(the instance of ChicksYack class), have the Chick object? Where did you create those objects? Check it again. If don't get it, ask again!
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
|
|
SCJP6.0,My blog Ranchers from Delhi
|
 |
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
|
|
I'll take a swing at it too. Each instance of ChicksYack will have its own 'c' member
variable of type Chicks. Three ChicksYack objects are created: #1 in the main method,
#2 and #3 in the go() method. When go() is called, it instantiates 'c' for ChicksYack
object #1. This 'c' is not used. It then starts ChicksYack #2 and #3 in their own threads.
The trick is that after each start(), the scheduler calls the run() method (not the go()
method) and run() tries to use 'c' without instantiating it.
Jim ... ...
|
BEE MBA PMP SCJP-6
|
 |
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
Object created in the lines 16,17(the instance of ChicksYack class), have the Chick object c.it is initialized to as
c = new Chicks();
THen why is it not accessible when run method of THreads created on line 16,17 is called.
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
{EDIT: This posted before mohitkumar gupta had edited his post above this.}
|
 |
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
|
|
mohitkumar : As I said above - please read carefully - there are three (3) separate
ChicksYack objects created, each with their own copy of 'c'. But only one Chicks object
is created. "c = new Chicks()" happens only if go() is called. This happens only once.
Jim ... ...
|
 |
Unmesh Chowdhury
Ranch Hand
Joined: Jun 20, 2010
Posts: 44
|
|
According to your ChicksYack class definition, there is a Chicks type reference variable in every instance of ChicksYack classes which is c.
When you create an instance of ChicksYack at line 12 the c reference variable of the instance gets null typically, after that, you invoke go() method on this instance, so in the go method the instance which is created at line 12 is involved. Now when you assigned a Chicks object in c at line 15, the involved instance’s c reference variable gets the value which was previously null. Now at line 16 and 17 you have created two instances of ChicksYack and their Chicks type references (individual c) are gets null typically and they are not assigned elsewhere but in both threads method is invoked on that references (those are not still refereed any actual Chicks object on the heap by their c reference variables) individually at line 20.
|
M.Sc. in CS, OCPJP6 93%
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
mohitkumar gupta wrote:Object created in the lines 16,17(the instance of ChicksYack class), have the Chick object c.it is initialized to as
c = new Chicks();
THen why is it not accessible when run method of THreads created on line 16,17 is called.
OK, in which instance you've called the go() method? And in which instances, you called the start() method and consequently the run() method? Please go through the this thread(post) again.
|
 |
jay sugrue
Greenhorn
Joined: May 29, 2012
Posts: 20
|
|
The answer to this question also says that if c were marked static and because yack is synchronized then the answers would be:
C: The output could be 4 4 2 2
E: The output could be 2 2 4 4
Is this because c does get initialized in the go method and if it was a static variable (just one copy) then the invocation of yack would
now be on an initialized variable, no matter the instance used ?
|
 |
Himai Minh
Ranch Hand
Joined: Jul 29, 2012
Posts: 287
|
|
When ChicksYack object 1 and 2 are created, they encapsulate Chick objects, c.
When new Thread(new ChicksYack()) starts run() method, the ChicksYack's c tries to call yack method. But this c is not initialized to any object. It is refering to null.
The c object in the code is not encapsulated by ChicksYack object 1 or 2.
|
 |
David Samer
Ranch Hand
Joined: Feb 08, 2012
Posts: 44
|
|
Hello everyone.
I hope this link : http://www.coderanch.com/t/596424/java-programmer-SCJP/certification/Threads-Resolved-test , can answer the doubts (in which I also asked the same question )
|
 |
 |
|
|
subject: Using non-static object as lock in Threads PRoblem
|
|
|