• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread question

 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why this code does not print 4444?
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well.. What does it print?

WP
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would it print 4444? It will be stuck in the hunter loop.
 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William P O'Sullivan wrote:Well.. What does it print?

WP

It does not print anything.
 
Henry Wong
author
Marshal
Pie
Posts: 21496
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21496
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21496
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic