logo

CuriousTab

CuriousTab

Discussion


Home Java Programming Threads Comments

  • Question
  • Which two statements are true?

    1. Deadlock will not occur if wait()/notify() is used
    2. A thread will resume execution as soon as its sleep duration expires.
    3. Synchronization can prevent two objects from being accessed by the same thread.
    4. The wait() method is overloaded to accept a duration.
    5. The notify() method is overloaded to accept a duration.
    6. Both wait() and notify() must be called from a synchronized context.


  • Options
  • A. 1 and 2
  • B. 3 and 5
  • C. 4 and 6
  • D. 1 and 3

  • Correct Answer
  • 4 and 6 

    Explanation
    Statements (4) and (6) are correct. (4) is correct because the wait() method is overloaded to accept a wait duration in milliseconds. If the thread has not been notified by the time the wait duration has elapsed, then the thread will move back to runnable even without having been notified.

    (6) is correct because wait()/notify()/notifyAll() must all be called from within a synchronized, context. A thread must own the lock on the object its invoking wait()/notify()/notifyAll() on.

    (1) is incorrect because wait()/notify() will not prevent deadlock.

    (2) is incorrect because a sleeping thread will return to runnable when it wakes up, but it might not necessarily resume execution right away. To resume executing, the newly awakened thread must still be moved from runnable to running by the scheduler.

    (3) is incorrect because synchronization prevents two or more threads from accessing the same object.

    (5) is incorrect because notify() is not overloaded to accept a duration.


    Threads problems


    Search Results


    • 1. What will be the output of the program?
      class MyThread extends Thread 
      {
          MyThread() 
          {
              System.out.print(" MyThread");
          }
          public void run() 
          {
              System.out.print(" bar");
          }
          public void run(String s) 
          {
              System.out.println(" baz");
          }
      }
      public class TestThreads 
      {
          public static void main (String [] args) 
          {
              Thread t = new MyThread() 
              {
                  public void run() 
                  {
                      System.out.println(" foo");
                  }
              };
              t.start();
          }
      }
      

    • Options
    • A. foo
    • B. MyThread foo
    • C. MyThread bar
    • D. foo bar
    • Discuss
    • 2. What will be the output of the program?
      class s1 extends Thread
      { 
          public void run() 
          { 
              for(int i = 0; i < 3; i++) 
              { 
                  System.out.println("A"); 
                  System.out.println("B"); 
              } 
          } 
      } 
      class Test120 extends Thread 
      { 
          public void run() 
          { 
              for(int i = 0; i < 3; i++) 
              { 
                  System.out.println("C"); 
                  System.out.println("D"); 
              } 
          } 
          public static void main(String args[]) 
              { 
              s1 t1 = new s1(); 
              Test120 t2 = new Test120(); 
              t1.start(); 
              t2.start(); 
          } 
      }
      

    • Options
    • A. Compile time Error There is no start() method
    • B. Will print in this order AB CD AB...
    • C. Will print but not be able to predict the Order
    • D. Will print in this order ABCD...ABCD...
    • Discuss
    • 3. What will be the output of the program?
      public class ThreadTest extends Thread 
      { 
          public void run() 
          { 
              System.out.println("In run"); 
              yield(); 
              System.out.println("Leaving run"); 
          } 
          public static void main(String []argv) 
          { 
              (new ThreadTest()).start(); 
          } 
      }
      

    • Options
    • A. The code fails to compile in the main() method
    • B. The code fails to compile in the run() method
    • C. Only the text "In run" will be displayed
    • D. The text "In run" followed by "Leaving run" will be displayed
    • Discuss
    • 4. The static method Thread.currentThread() returns a reference to the currently executing Thread object. What is the result of this code?
      class Test 
      {
          public static void main(String [] args) 
          {
              printAll(args);
          }
      
          public static void printAll(String[] lines) 
          {
              for(int i = 0; i < lines.length; i++)
              {
                  System.out.println(lines[i]);
                  Thread.currentThread().sleep(1000);
              }
          }
      }
      

    • Options
    • A. Each String in the array lines will output, with a 1-second pause.
    • B. Each String in the array lines will output, with no pause in between because this method is not executed in a Thread.
    • C. Each String in the array lines will output, and there is no guarantee there will be a pause because currentThread() may not retrieve this thread.
    • D. This code will not compile.
    • Discuss
    • 5. What will be the output of the program?
      class s implements Runnable 
      { 
          int x, y; 
          public void run() 
          { 
              for(int i = 0; i < 1000; i++) 
                  synchronized(this) 
                  { 
                      x = 12; 
                      y = 12; 
                  } 
              System.out.print(x + " " + y + " "); 
          } 
          public static void main(String args[]) 
          { 
              s run = new s(); 
              Thread t1 = new Thread(run); 
              Thread t2 = new Thread(run); 
              t1.start(); 
              t2.start(); 
          } 
      }
      

    • Options
    • A. DeadLock
    • B. It print 12 12 12 12
    • C. Compilation Error
    • D. Cannot determine output.
    • Discuss
    • 6. The following block of code creates a Thread using a Runnable target:
      Runnable target = new MyRunnable();
      Thread myThread = new Thread(target);
      
      Which of the following classes can be used to create the target, so that the preceding code compiles correctly?

    • Options
    • A. public class MyRunnable extends Runnable{public void run(){}}
    • B. public class MyRunnable extends Object{public void run(){}}
    • C. public class MyRunnable implements Runnable{public void run(){}}
    • D. public class MyRunnable implements Runnable{void run(){}}
    • Discuss
    • 7. Which statement is true?

    • Options
    • A. If only one thread is blocked in the wait method of an object, and another thread executes the modify on that same object, then the first thread immediately resumes execution.
    • B. If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, it is still possible that the first thread might never resume execution.
    • C. If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, then the first thread definitely resumes execution as a direct and sole consequence of the notify call.
    • D. If two threads are blocked in the wait method of one object, and another thread executes the notify method on the same object, then the first thread that executed the wait call first definitely resumes execution as a direct and sole consequence of the notify call.
    • Discuss
    • 8. Which statement is true?

    • Options
    • A. The notifyAll() method must be called from a synchronized context.
    • B. To call wait(), an object must own the lock on the thread.
    • C. The notify() method is defined in class java.lang.Thread.
    • D. The notify() method causes a thread to immediately release its locks.
    • Discuss
    • 9. Which statement is true?

    • Options
    • A. A static method cannot be synchronized.
    • B. If a class has synchronized code, multiple threads can still access the nonsynchronized code.
    • C. Variables can be protected from concurrent access problems by marking them with the synchronized keyword.
    • D. When a thread sleeps, it releases its locks.
    • Discuss
    • 10. Which two can be used to create a new Thread?

      1. Extend java.lang.Thread and override the run() method.
      2. Extend java.lang.Runnable and override the start() method.
      3. Implement java.lang.Thread and implement the run() method.
      4. Implement java.lang.Runnable and implement the run() method.
      5. Implement java.lang.Thread and implement the start() method.

    • Options
    • A. 1 and 2
    • B. 2 and 3
    • C. 1 and 4
    • D. 3 and 4
    • Discuss


    Comments

    There are no comments.

Enter a new Comment