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

Code is not working as expected

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is my code. I am expecting t1 and t2 should run in parallel but after completion of t1 , t2 is beginning. Am I doing anything wrong . Actually I am trying to replicate producer and consumer problem but some where I am making some mess.

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I ran it, the output was:


So they did run in parallel. The problem is that withdraw only went through the loop once which is probably not what you wanted.

The reason is it looks at the size of amount once at the beginning of the for loop so it doesn't see the new items.

Also, note that ArrayList is not threadsafe. Consider using a concurrent collection.
 
somashaker goud
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Jenne - if the reason is what you mentioned "The reason is it looks at the size of amount once at the beginning of the for loop so it doesn't see the new items. " then it should atleast print sysout . I mean to say run method of Withdraw is getting called once
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

somashaker goud wrote:...then it should atleast print sysout . I mean to say run method of Withdraw is getting called once

Hi goud,
The first thing to understand is the 2 threads t1 and t2 will run in a unpredictable manner i.e.,you can't predict which will start running earlier or later,actually its totally the job of the thread scheduler to schedule them.
In your case,for simplicity lets take the output given by jeanne.accordingly,first the run method of deposit is called where it puts the 1st element and go to sleep for 1 sec,now in the meantime run method of withdraw gets called and it founds the size of amount =1 so condition checks happened i<1 which is 0<1=true so iterates once and removes the element and sets the size=0 again and frequently after it goes to sleep for 1 sec now again in the meantime t1 gets awake and again it puts the element in the amount which causes to increase the size of amount from 0 to 1 and goes to sleep for 1 sec now again t2 awakes who found size of amount =1 now this time it needs to check the condition again which is i<size or i<1 or  due to second iteration it is 1<1(as i is initialized with value 0) where it founds the condition=false so the loop breaks and the thread t2 dies and after it t1 continues as per condition.

Note:their may be the case where the loop of t2 will not iterate for once as suppose if the t2 starts first and size of amount =0 so the condition i<size will be 0<0 which is false and thus it will iterate for even once.but it depends totally on thread scheduler as i allready mentioned.

Hope it helps!!!

Kind Regards,
Praveen.
 
author
Posts: 23951
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
Nicely detailed post Praveen -- have a cow.

somashaker goud wrote:@Jenne - if the reason is what you mentioned "The reason is it looks at the size of amount once at the beginning of the for loop so it doesn't see the new items. " then it should atleast print sysout . I mean to say run method of Withdraw is getting called once



As a more shorter explanation, the reason the for loop "doesn't see the new items" is because it completed before the new items were added -- meaning the loop finished. So, what do you mean by "should atleast print sysout"? There aren't any more sysout after the loop.

Henry
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the cattle,henry.

Kind Regards,
Praveen.
 
somashaker goud
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry and Praveen for your explanations
 
reply
    Bookmark Topic Watch Topic
  • New Topic