This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread question " Watch "Thread question " New topic
Author

Thread question

Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136

why this code does not print 4444?
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

Well.. What does it print?

WP
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2065
    
  22

Why would it print 4444? It will be stuck in the hunter loop.
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
I thought every time rabbit gets incremented (since, all thread are using the same object) and with the last thread access it will set to 4 and shifter will set to true and then hunter method runs and print 4444.
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
William P O'Sullivan wrote:Well.. What does it print?

WP

It does not print anything.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39

Janki Shah wrote:I thought every time rabbit gets incremented (since, all thread are using the same object) and with the last thread access it will set to 4 and shifter will set to true and then hunter method runs and print 4444.


All of your Tazo instances does share a single tazo object, but it is not using the rabbit variable from that object. It is using its own copy.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39

Janki Shah wrote:
William P O'Sullivan wrote:Well.. What does it print?

WP

It does not print anything.


which was explain earlier....

Jayesh A Lalwani wrote:Why would it print 4444? It will be stuck in the hunter loop.


Henry
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
Henry Wong wrote:
Janki Shah wrote:I thought every time rabbit gets incremented (since, all thread are using the same object) and with the last thread access it will set to 4 and shifter will set to true and then hunter method runs and print 4444.


All of your Tazo instances does share a single tazo object, but it is not using the rabbit variable from that object. It is using its own copy.

Henry

That means every time thread locks on tazo object , tazo will get all new Tazo(); and all the instance variable in Tazo class? tazo-->tazox-->new Tazo();
If I am wrong can you explain little more, please.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39

Janki Shah wrote:That means every time thread locks on tazo object , tazo will get all new Tazo(); and all the instance variable in Tazo class? tazo-->tazox-->new Tazo();
If I am wrong can you explain little more, please.


Huh??? How does locking an object creates another object? Can you please elaborate?

Henry
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
Henry Wong wrote:
Janki Shah wrote:That means every time thread locks on tazo object , tazo will get all new Tazo(); and all the instance variable in Tazo class? tazo-->tazox-->new Tazo();
If I am wrong can you explain little more, please.


Huh??? How does locking an object creates another object? Can you please elaborate?

Henry

Sorry!
No no, that's not what I meant. What I meant is "tazo will get new Tazo(); and all the instance variable in Tazo class? That's all." and "Threads get lock on a single Object tazo."
once again sorry about that!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread question
 
Similar Threads
What's wrong with this code? (trying wait and notifyall for first time...)
Deadlock on CertPal question
explain a code from k&b
Blocking an InputStream
Illegal Monitor State Exception