Win a copy of Functional Design and Architecture this week in the Functional programming forum!
  • 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

multithreading

 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

output is: foowiith90.0
barwith90.0
foodepo100.0
bardepo100.0
(first two lines are not sure, any thread can run first)
my question is : if we have put the call to deposit() and withdraw() in the synchronized block, why not it is generating lock for both the threads, foo and bar. it should print for one thread first and then runs for the another thread ??
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:
my question is : if we have put the call to deposit() and withdraw() in the synchronized block, why not it is generating lock for both the threads, foo and bar. it should print for one thread first and then runs for the another thread ??



Your transfer method only transfers from the "from" account to the "from" account... and, both threads are working with different "from" accounts.

Henry
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
was just editing my post that I have understood why it is running so, and before me you replied.
thank you Henry for such a quick response
one more doubt I have
why there is deadlock if we edit the code :
 
Henry Wong
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:
one more doubt I have
why there is deadlock if we edit the code :



This is a classic deadlock example.

One thread locks the "from" account (foo). Then proceeds to do a withdrawal.

The other thread locks the "from" account (bar). Then proceeds to do a withdrawal.

The first thread tries to lock the "to" account (bar)... but blocks as the other thread has the lock.

The other thread tries to lock the "to" account (foo)... but blocks as the first thread has the lock.

Neither thread can continue until the other thread releases the required lock. And neither will release the required lock, as neither thread can continue.

Henry
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
The first thread tries to lock the "to" account (bar)... but blocks as the other thread has the lock.


this is my doubt !
you said that "other thread has the lock" - which *other*thread has already the lock and how ?? please elaborate


and one more thing I want to ask, I am stuck in the threads form last 1 week, and the level I have reached is in front of you. Really I am getting down as nothing is going perfectly in my head. Java Thread by you , will be helpful for me or not?? I want a study guide that starts for beginner and cover almost every important topics . Please tell me !!
Thank you
 
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic