| Author |
Synchronised Vs. Non synchronised Thread
|
Nabila Mohammad
Ranch Hand
Joined: Nov 05, 2007
Posts: 661
|
|
This is exactly the same exampl given in K & B book. When I run this program with out synchronising the method , Fred and Lucy take turns in an irregular pattern (as expected) But when I run this program with the keyword Synchronised exactly as mentioned in the above code - I donot see them taking turns. Fred makes 5 transacation in a sequence till there is no Balance left. Then Lucy makes the next five transaction in a sequence which gives the output "Not enough moeny for Lucy to withdraw 0" 5 times in a sequence. It looks like when I donot synchronise the method , both the threads are woking simultaneously but when I Synchronize the method, the second thread only starts when the first thread is completed. Is this how it is supposed to work ???
|
The future belongs to those who believe in the beauty of their dreams.Dream BIG!
|
 |
Noam Wolf
Ranch Hand
Joined: Jan 12, 2008
Posts: 35
|
|
I don't see the same result as you do. When I run your code I see the following output: Fred is going to withdraw Fred completes withdrawal. Now the balance left is 40 Lucy is going to withdraw Lucy completes withdrawal. Now the balance left is 30 Fred is going to withdraw Fred completes withdrawal. Now the balance left is 20 Lucy is going to withdraw Lucy completes withdrawal. Now the balance left is 10 Fred is going to withdraw Fred completes withdrawal. Now the balance left is 0 Not enough money in account for Lucy to withdraw 0 Not enough money in account for Fred to withdraw 0 Not enough money in account for Lucy to withdraw 0 Not enough money in account for Fred to withdraw 0 Not enough money in account for Lucy to withdraw 0
|
because .net guys can also write in java
|
 |
Nabila Mohammad
Ranch Hand
Joined: Nov 05, 2007
Posts: 661
|
|
That's what's written on the book and that's what I was expecting or something similiar.. But I am not getting that. The out put I am getting is that the thread completes one after the other and NOT simultaneosly.
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2562
|
|
The out put I am getting is that the thread completes one after the other and NOT simultaneosly.
synchronization is meant for the same purpose only one thread at a time so the output you are getting is also correct. and remember the words from K & B book, in thread execution nothing can be guranteed Hope this helps
|
SCJP, SCWCD.
|Asking Good Questions|
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2562
|
|
and yes the topic name reminds me of one more thing that is thread is not synchronized the code is. Hope this helps
|
 |
Nabila Mohammad
Ranch Hand
Joined: Nov 05, 2007
Posts: 661
|
|
Originally posted by Amit Ghorpade: in thread execution nothing can be guranteed
Yes, I do understand that - but I thought we could predict it to some extent . Isn't that the whole purpose of having Threads is that they work simulataneously and independent of one another. And NOT one after the other. Wonder if this has something to do with JVM...
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2562
|
|
To some extent you are correct that you want concurrency and not serial execution. But remember that you can never even predict thread behavior because its entirely based on the thread schedular. Just try using more than two threads, maybe you'll get closer to your desired output. Hope this helps
|
 |
 |
|
|
subject: Synchronised Vs. Non synchronised Thread
|
|
|