• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Confusing Multithreaded output

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand, that little is guaranteed where multithreading is concerned. But i am not able to understand the output which i am getting for the below program.
acc is a shared object by these threads...
output:
Fred is going to withdraw. Current balance :50
Lucy is going to withdraw. Current balance :50
Lucy completes withdrawal, now Current balance :30
Lucy is going to withdraw. Current balance :30
Lucy completes withdrawal, now Current balance :10
Lucy is going to withdraw. Current balance :10
Lucy completes withdrawal, now Current balance :-10
Fred completes withdrawal, now Current balance :30 why is it 30 and not -30???
Fred is going to withdraw. Current balance :30
Fred completes withdrawal, now Current balance :10
Fred is going to withdraw. Current balance :10
Fred completes withdrawal, now Current balance :-10

Please help.
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hallo Rani,
you make a loop of 3 iterations, starting from 50, subtracting 20 everytime for each thread.
How could you expect a -30?

If you start from 50

first iteration 50 - 20 = 30

second iteration 30 - 20 = 10

third iteration 10 - 20 = -10

That's all.

You put to sleep thread named Fred for 2 seconds before the loop begins, so Lucy probably will be the first to complete method run. So it happens that while Fred is sleeping,Lucy complete her loop starting from 50 (30,10,-10) , then it's Fred's turn.
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Rani, i forgot to clear this.

When you write



you create two instances of class ThreadSynch1, each instance has its own Account instance.
You are not sharing the Account, you have two copies of it, two separate objects.
 
Rani Karunakaran
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh Yes, thanks...
 
Marshal
Posts: 79808
388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Please always use the code button for code. I shall try and edit your first post so you can see how much better it looks.
 
I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic