• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Yielding...

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a question with regard to the code below.

Its a standard scenario of a bank account with 2 people withdrawing cash if the funds are available.
The output varies with whether the Person yields or not.

If there is no yielding then the output is as follows
a does all the withdrawing

a 0 withdrawing $20; balance is 80
a 1 withdrawing $20; balance is 60
a 2 withdrawing $20; balance is 40
a 3 withdrawing $20; balance is 20
a 4 withdrawing $20; balance is 0

If I uncomment the yielding then a does the first withdrawl, yields and then b does all the rest

a 0 withdrawing $20; balance is 80
b 0 withdrawing $20; balance is 60
b 1 withdrawing $20; balance is 40
b 2 withdrawing $20; balance is 20
b 3 withdrawing $20; balance is 0

If I uncomment the yielding line and the line that prints who is yielding then the withdrawls alternate

a 0 withdrawing $20; balance is 80
a is yielding...
b 0 withdrawing $20; balance is 60
b is yielding...
a 1 withdrawing $20; balance is 40
a is yielding...
b 1 withdrawing $20; balance is 20
b is yielding...
a 2 withdrawing $20; balance is 0
a is yielding...
b is yielding...
a is yielding...
b is yielding...

This final output is what i would have expected with yielding alone and am wondering why the addition of the print statement changes things.

Does anyone have any ideas?

thanks

j

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are running a loop which runs 4 times. So the yeild statement is running 4 times.

Try adding the else block for the if(accoutbalance<20) with return or break then it may not show.
 
jolyon holdstock
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijay,

Thanks for the reply.

Its not that the line is printed, its about the order that the threads are being processed.

By inserting the yield command I expected another thread chosen at random to proceed but the order of execution is always the same. So i don't understand how the yield is working; also including the print statement affects the thread chosen to proceed.

cheers,

J
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when calling yield(); you allow other threads with higher priority to be executed, if no other threads exsist with higher priority, a thread at random will be picked.
Without knowing what, i suspect that something in your code is causing the executing thread to get a higher priority then the other thread.
try adding a getPriority() call to your code, might be fun to se what the result is
 
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic