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.
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.
Never Never Never Give Up...!!!
Joined: May 03, 2007
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.
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
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com