Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread question: Please help  RSS feed

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What I think:
I understand it in the way that both run() and setPrice() are synchronized
so setPrice() and run() can't be called concurrently by one thread.

Please elaborate the output:
Is it guaranteed output?



Source of this question


Regards,
cmbhatt
 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From looking at it, this will end in an endless loop printing 100.
The run method never leaves the while(true)-loop

Good luck.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The run method never leaves the while(true)-loop



So what about scheduling, if one is consuming all the CPU time???
why wont setPrice() method called.

On my machine it is printing 100, endlessly.


Regards,
cmbhatt
[ April 19, 2007: Message edited by: Chandra Bhatt ]
 
Steven Young
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It prints 100 endlessly on my laptop.

This is how I think the code is working:

1. Main thread can not update the Slave object's instance variable iPrice until the new thread runs the Slave object. The "while(bContinue==false){ }" statement makes sure of this.

2. The new thread running Slave object sets the bContinue to true, but because the run() method is synchronized, this thread will hold a lock on the Slave object. The run() method has an infinite loop that prints out the value of iPrice (100).

3. Because the Slave object's setPrice method is synchronized, the Main thread can never get a lock on the Slave object, because the thread running the Slave object is stuck in an infinite loop. So the main thread sits there in blocked mode, waiting for the lock to be released.

So 100 will be endlessly printed. Even if you are running a machine with dual processors, because the Slave object is locked and remains locked when Master's bContinue is set to true, I can't see how the Main thread ever gets to set the slave's iPrice to 200.

Am I missing something ??
 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

it prints also 100 endlessly on my computer.

Steven,
I see it the same way.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandra,

The program is gives gurenteed output.

Beucase of snippet: after thread start,

while(bContinue==false){}

If you remove the above condition your program behaves differenly in windows.

1.Why becuase main finishes first.
price set 200.

2. After that thread runs printing 200.

Check it out.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following variation also causes to 200 to be printed.

 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kieth,

Thats really smart way to look into.
I appreciate it.

But still we can improvise it. By waiting for a purpose.

try{

if (iPrice < 200) { wait(10000); }
}catch (InterruptedException ie) { }

[ April 19, 2007: Message edited by: Srinivasan thoyyeti ]
[ April 19, 2007: Message edited by: Srinivasan thoyyeti ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your kind effort!

Keith, your code gives a good idea.

By the way, I removed the synchronized from both the method,
Resultantly it is printing 200 endlessly.

Do I get any clue, after removing synchronized from the setPrice()
method?

Oh,main() was blocked to call the setPrice(), because both
methods were synchronized, until run completes, setPrice couldn't
be called. Is that correct???


Please confirm!!!

Thanks,
cmbhatt
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chandra Bhatt:

What I think:
I understand it in the way that both run() and setPrice() are synchronized
so setPrice() and run() can't be called concurrently by one thread.

Please elaborate the output:
Is it guaranteed output?

Because of while(true) in the Slave class' run() the loop never ends and because of this, the thread never dies. The thread also never gets a chance to run setPrice() method because it never comes out of the run() because of the endless loop in it. Main thread too never gets a chance to run and finish. Probably, because of the slave thread too busy executing the loop.
setPrice() and run() are synchronized on the same object. But, because the thread is not allowed to come out of the run() setPrice() never gets chance to execute.
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chandra Bhatt:
Thank you all for your kind effort!

Keith, your code gives a good idea.

By the way, I removed the synchronized from both the method,
Resultantly it is printing 200 endlessly.

Do I get any clue, after removing synchronized from the setPrice()
method?

Oh,main() was blocked to call the setPrice(), because both
methods were synchronized, until run completes, setPrice couldn't
be called. Is that correct???


Please confirm!!!

Thanks,
cmbhatt


You removed synchronized from both methods. Alright.
Did you observe s.setPrice(200) in main()? It's setting the iPrice=200 right? Now that the iPrice is 200, the code endlessly prints the value of iPrice inside the loop present in run().
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Faisal,


You removed synchronized from both methods. Alright.


I removed the synchronized from setPrice(), now main is not blocked, it can
call the setPrice() method. Right???



Regards,
cmbhatt
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chandra Bhatt:
Faisal,

I removed the synchronized from setPrice(), now main is not blocked, it can
call the setPrice() method. Right???

Regards,
cmbhatt


Unforutnately, you are also no longer thread safe. A better option is keep setPrice() synchronized. Create a new (private) method, call getPrice(), and have that also syncronized. And change the run method to use the getPrice() method, instead of accessing the price directly, and remove the synchronization from the run() method.

Henry
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Thanks Henry!


EDITED: THANKS Faisal, "My brain had gone dumb" "made instance variable iPrice private."

Anything else, please tell!


Regards,
cmbhatt
[ April 19, 2007: Message edited by: Chandra Bhatt ]
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey c'mon yaar!
Make instance variables private not the setter & getter!
Make iPrice private. ok?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!