|
![]() |
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
Answer:
✓ F is correct. When run() is invoked, it is with a new instance of ChicksYack and c has
not been assigned to an object. If c were static, then because yack is synchronized, answers
C and E would have been correct.
A, B, C, D, and E are incorrect based on the above. (Objective 4.3)
Praveen Kumar M K wrote:In your main method, what would be the output if there were just this code,
David Samer wrote:Hey hello there Praveen,
Praveen Kumar M K wrote:In your main method, what would be the output if there were just this code,
Hmm, that means chicksYack is trying to manipulate c object , but... wait.. if its inside main... its static method, so c should be static and initialized... is this the issue? then... hmm... no access.
David Samer wrote:Greetings there community
![]()
I am hell confuse with the following test question and its explanation, from Kathy's Sierra and Bert's Book (Chapter 9 - Threads, page 784 )
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
Answer:
✓ F is correct. When run() is invoked, it is with a new instance of ChicksYack and c has
not been assigned to an object. If c were static, then because yack is synchronized, answers
C and E would have been correct.
A, B, C, D, and E are incorrect based on the above. (Objective 4.3)
:/
I see when run() is invoked, somehow takes instance of ChicksYack and , here the part I'm confuse with, it says c ... , why has not been assigned to an object? (What it does mean? ). (Isn't c instantiated already once thread invokes run()?)( Looks like it c weren't into the scope for run() but , no clue why )
Thanks in advance .
Praveen Kumar M K wrote:
c need not be static and there is no access issue, however you are correct to point out that c should be initialized. We see that creating a new ChicksYack object does not inturn create a new Chicks object. Only a reference called c is created. Now, if we were to call the yack function on this reference, without initialization we would get a null pointer exception. Is that ok?
Praveen Kumar M K wrote:
Now track back to the original program and see where a new Chicks object gets created and is assigned to "c". Let me know what you understand.
Praveen Kumar M K wrote:
It looks like c has been assigneed to an object, but that one belongs to the main thread. When it tries to run c.yack(Thread.currentThread().getId()), it needs a "c" which has been assigned to the new thread. In other words, "c" has not been initialized in the context. On the other hand, if c is static, it belongs to the class, not any individual instance, so we only need to instantiate it once.
Hope it answers your question
David Samer wrote:
As for what I see, tracking back, line 17 is the one in where Chicks object is created at the same time assigned to "c" . Fine, so far, but now it comes the confuse part for me. I suppose program keeps goinng so after line 17 , it creates another 2 object, but this time, both are Threads ,which both gets started but. In which point , can I know it will invoke run() and call yack(long id) method , as well as on which object?
Praveen Kumar M K wrote:
Right, there are 2 new ChicksYack objects. But have we called the go method on these objects to initialize their internal Chicks objects? Nope, their "c" is still null.
Its a kind of catch-22 situation. If you actually did something like
you would have a recursive overflow..and without the call you would have a null pointer exception! A possible change would be as Zhenyi Luo suggested, to make c static.
Praveen Kumar M K wrote:The thread "id" AFAIK is just some autogenerated number uniquely identifying a thread. I think if you run your program again maybe you'll get a different output.
Sometimes you feel like a nut. Sometimes you feel like a tiny ad.
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
|