About Question enthuware.ocpjp.v7.2.1407 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ewebxml
Posts: 77
Joined: Sun Jun 30, 2013 10:04 pm
Contact:

About Question enthuware.ocpjp.v7.2.1407 :

Post by ewebxml »

For the following code the word "done" is never printed.

Code: Select all

public class MySecureClass
{
   public synchronized void doALotOfStuff()
   {
      try
      {
         LINE1: Thread.sleep(10000);
      }catch(Exception e){ }
   }
   public synchronized void doSmallStuff()
   {
      System.out.println("done");
   }
   public static void main(String args[]){
	   MySecureClass mySC = new MySecureClass();
	   mySC.doALotOfStuff();
   }
}
/* */
Is there something wrong with the way that I am calling the method
doALotOfStuff();?
-----
I selected option c)
done will never be printed

because for the code above "done" is never printed.



Please confirm.

admin
Site Admin
Posts: 9170
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1407 :

Post by admin »

The question says, "Assume that there are two threads. Thread one is executing the doALotOfStuff() method and has just called LINE 1 and is sleeping. Now, Thread two decides to call doSmallStuff() method on the same object."

Your code does not do what the question asks. Your main method should be something like this:

Code: Select all

  public static void main(String args[]){
      final MySecureClass mySC = new MySecureClass();
      Thread t1 = new Thread(){
        public void run(){
          mySC.doALotOfStuff();
        }
      };
      t1.start(); 
      Thread t2 = new Thread(){
        public void run(){
          mySC.doSmallStuff();
        }
      };
      t2.start();
   }

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

ewebxml
Posts: 77
Joined: Sun Jun 30, 2013 10:04 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1407 :

Post by ewebxml »

I understand that this question specifically states that there are 2 threads.
Q: Can you confirm that the same output will be generated with 1 thread (as in main below),
although this is what is not stated(or required) by the question?

Code: Select all

public class MySecureClass {
  public synchronized void doALotOfStuff() {
    System.out.println("Witin doALotOfStuff()");
      try {
        LINE1: Thread.sleep(10000);
      } catch (Exception e) {}
  }
  public synchronized void doSmallStuff() {		
    System.out.println("done");
  }
  /* main method with 1 thread*/
  public static void main(String args[]) {
    Thread t1 = new Thread(new Runnable() {
      public void run() {
        MySecureClass m1 = new MySecureClass();
        m1.doALotOfStuff();
        m1.doSmallStuff();
      }
    });
    t1.start();
  }
}
/* 
Program Output 
Witin doALotOfStuff()
done		// Note: This line is printed 10 seconds later
*/

admin
Site Admin
Posts: 9170
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1407 :

Post by admin »

Yes, the output will be the same.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests