• 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

Using non-static object as lock in Threads PRoblem

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
author
Posts: 23956
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

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
 
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
This question has already discussed recently.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23956
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

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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ... ...
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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
{EDIT: This posted before mohitkumar gupta had edited his post above this.}
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ... ...
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
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

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.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?

 
Bartender
Posts: 2436
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.

 
Ranch Hand
Posts: 49
Eclipse IDE Spring Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone.

I hope this link : https://coderanch.com/t/596424/java-programmer-SCJP/certification/Threads-Resolved-test , can answer the doubts (in which I also asked the same question )

 
reply
    Bookmark Topic Watch Topic
  • New Topic