Note that there are some explanatory texts on larger screens.

plurals
  1. POElement has no attached source code Java.Lang
    primarykey
    data
    text
    <p>Hello I am trying to use the Double Object, rather then the type in order to synchronize threads. This is a homework assignment.</p> <p>Overview: This application is a trivial example of a banking system, it has thread that run as the depositors and threads that run as the withdrawers. depositors add 10$ and withdrawers remove 10$ so the end of the application should have 10 bank accounts that are unchanged.</p> <p>In each class (depositors and withdrawers) i was able to use block synchronization on the account object to accomplish the goal. So the homework part of my assignment is finished, and i don't need help with that.</p> <p>I got to thinking, and i feel like locking the entire Account object is overkill. Since the only real part of the transaction that is critical is changing the variable</p> <pre><code> balance </code></pre> <p>originally the variable balance was a double. My goal, was to change the variable to the Double object, and then move the synchronization block to the inside the account object and only around the parts of the code that modify the balance variable.</p> <p>When i changed the code to Double, there is no error but i get a note:</p> <pre><code>This element has no attached source and the Javadoc could not be found in the attached Javadoc </code></pre> <p>Also, the code does not work as my values come out all wrong at the end.</p> <p>Questions:</p> <ol> <li>Is my theory correct, that i should be able to lock only the Balance variable?</li> <li>How can i get my code to recognize the Java.lang code for Double?</li> </ol> <p>Thanks</p> <p>Here is some code... </p> <p>Declaration:</p> <pre><code>private Double balance; </code></pre> <p>Constructor:</p> <pre><code>this.balance = new Double(balance); </code></pre> <p>Locking:</p> <pre><code>synchronized(balance){ balance = balance + amount; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload