• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Threads

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


What will be account number field's maximum value?
 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dishi Jain wrote:

What will be account number field's maximum value?




Please tell us what output you get when running the code. Is this output different than what you think the output should be?
 
Dishi Jain
Ranch Hand
Posts: 46
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes the loop ends with account number value = 2000 but sometimes it ends with value less than 2000 for eg. 1999.
According to me every time the last running thread should stop with account number value = 2000 but no less.
 
author
Posts: 23928
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

Dishi Jain wrote: Sometimes the loop ends with account number value = 2000 but sometimes it ends with value less than 2000 for eg. 1999.
According to me every time the last running thread should stop with account number value = 2000 but no less.



The place to focus on here is this line...



Basically, the getNumber() method is synchronized, and hence, the call to the method is atomic; the setNumber() method is synchronized, and hence, the call to the method is atomic.

However, there is no synchronization that spans both calls simultaneously, and hence, the complete expression is not guaranteed to be atomic. It is possible for one thread to read a number (say number 42), increment it, but before it can set the new number (43), the other thread reads the same number 42.

Henry
 
Dishi Jain
Ranch Hand
Posts: 46
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Dishi Jain wrote: Sometimes the loop ends with account number value = 2000 but sometimes it ends with value less than 2000 for eg. 1999.
According to me every time the last running thread should stop with account number value = 2000 but no less.



The place to focus on here is this line...



Basically, the getNumber() method is synchronized, and hence, the call to the method is atomic; the setNumber() method is synchronized, and hence, the call to the method is atomic.

However, there is no synchronization that spans both calls simultaneously, and hence, the complete expression is not guaranteed to be atomic. It is possible for one thread to read a number (say number 42), increment it, but before it can set the new number (43), the other thread reads the same number 42.

Henry



Thanks Henry,

I get it that why the value might be less than 2000
But now when the previous thread will come back, will it set the account number value which it gets last time..??
And everything what the other thread has done will be lost?
 
Henry Wong
author
Posts: 23928
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

Dishi Jain wrote:
But now when the previous thread will come back, will it set the account number value which it gets last time..??
And everything what the other thread has done will be lost?



Not sure what you are asking? Are you asking if there are cases where it might be more than 2000?

Henry
 
Dishi Jain
Ranch Hand
Posts: 46
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Dishi Jain wrote:
But now when the previous thread will come back, will it set the account number value which it gets last time..??
And everything what the other thread has done will be lost?



Not sure what you are asking? Are you asking if there are cases where it might be more than 2000?

Henry



No. I mean if :

1. Thread_0 upon getNumber() retrieves 42 and then leaves the lock.
2. Then Thread_1 comes which also retrieves 42, and sets it 43 and runs for next 7 times increasing number value upto let say 50 and now leaves the lock.
3. Now Thread_0 is back.

In above scenario will the number field will again be set as 43 by Thread_0, since what it read lastly was 42, making the other thread losing its changes. ??
 
Henry Wong
author
Posts: 23928
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

Dishi Jain wrote:
In above scenario will the number field will again be set as 43 by Thread_0, since what it read lastly was 42



yes
 
Dishi Jain
Ranch Hand
Posts: 46
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Dishi Jain wrote:
In above scenario will the number field will again be set as 43 by Thread_0, since what it read lastly was 42



yes



Thanks Henry
 
They weren't very bright, but they were very, very big. Ad contrast:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic