Java OCA OCP Practice Question 2191

Question

Given the following program, which alternatives would make good choices to synchronize on at (1)?

public class Preference {
  private int account1;
  private Integer account2;

  public void doIt() {
    final Double account3 = new Double(10e10);
    synchronized(/* ___(1)___ */) {
      System.out.print("doIt");
    }/* w  w  w. j  a  v a  2  s . c  o  m*/
  }
}

    

Select the two correct answers.

  • (a) Synchronize on account1.
  • (b) Synchronize on account2.
  • (c) Synchronize on account3.
  • (d) Synchronize on this.


(b) and (d)

Note

We cannot synchronize on a primitive value.

Synchronizing on a local object is useless, as each thread will create its own local object and it will not be a shared resource.




PreviousNext

Related