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.