aspose file tools*
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
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: 859

Well.. What does it print?

WP
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2377
    
  28

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: 18845
    
  40

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: 18845
    
  40

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: 18845
    
  40

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread question