Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronised Vs. Non synchronised Thread

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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 ???
 
Noam Wolf
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic