SCJP考试中的线程题目分析

来源:java认证发布时间:2012-11-12 12:48:03java认证视频


  5:
  1.12 Which of the following will definitely stop a thread from executing?A. wait()B. notify()C. yield()D. suspend()E. sleep()Answer: ACDE
  6:
  1.12 Which of the following will definitely stop a thread from executing?
  A. wait()
  B. notify()
  C. yield()
  D. suspend()
  E. sleep()
  Answer: ACDE
  7:
  Which of the following statements about threading are true?
  A. You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements runnable.
  B. You can obtain a mutually exclusive lock on any object.
  C. You can't obtain a mutually exclusive lock on any object.
  D. Thread scheduling algorithms are platform dependent.
  Answer: BD
  8:
  Consider the following statement:
  Thread myThread = new Thread();
  Which of the following statements are true regarding myThread?
  A. The thread myThread is now in a runnable state.
  B. The thread myThread has a priority of 5.
  C. On calling the start() method on myThread, the run method in the Thread class will be executed.
  D. On calling the start() method on myThread, the run method in the calling class will be executed.
  Answer: C
  Note: the priority of myThread will be inherited from the Thread that called the constructor.
  9:
  What is the effect of issuing a wait() method on an object?(Mutiple)
  1) If a notify() method has already been sent to that object then it has no effect
  2) The object issuing the call to wait() will halt until another object sends a notify() or notifyAll() method
  3) An exception will be raised
  4) The object issuing the call to wait() will be automatically synchronized with any other objects using the receiving object.
  ANSWER 1)
  10:
  Pick all the true statements below.
  1) If a thread wants to call wait() on an object, the thread must own that object's lock.
  2) There is a method that you can call on an instance of the Thread class that puts the instance to sleep for a specified  number of milliseconds.
  3) At the moment when a thread is notified, it automatically gets the lock of the object for which it was waiting.
  ANSWER 1
  11:
  1. class Z {
  2.  public static void main(String[] args) {
  3.   new Z();
  4.  }
  5.
  6.  Z() {
  7.   Z alias1 = this;
  8.   Z alias2 = this;
  9.   synchronized(alias1) {
  10.   try {
  11.   alias2.wait();
  12.   System.out.println("DONE WAITING");
  13.   }
  14.   catch (InterruptedException e) {
  15.   System.out.println("INTERRUPTED");
  16.   }
  17.   catch (Exception e) {
  18.   System.out.println("OTHER EXCEPTION");
  19.   }
  20.   finally {
  21.   System.out.println("FINALLY");
  22.   }
  23.   }
  24.   System.out.println("ALL DONE");
  25.  }
  26. }
  Mutiple:
  1) Compiler error.
  2) The application compiles but doesn't print anything.
  3) The application prints "DONE WAITING".
  4) The application prints "INTERRUPTED".
  5) The application prints "OTHER EXCEPTION".
  ANS: 2)
  12:
  What will happen when you attempt to compile and run the following code?
  class MyThread extends Thread{
  public void run(){
  System.out.println("MyThread: run()");
  }
  public void start(){////注意,此方法重写后,不再有CALL RUN()方法的功能。
  System.out.println("MyThread: start()");
  }
  }
  class MyRunnable implements Runnable{
  public void run(){
  System.out.println("MyRunnable: run()");
  }
  public void start(){//Runnable 并无此方法,所以,除非本类的对象可以CALL 它,否则,将CALL THREAD 的start()执行run()
  System.out.println("MyRunnable: start()");
  }
  }
  public class MyTest {
  public static void main(String args[]){
  MyThread myThread = new MyThread();
  MyRunnable myRunnable = new MyRunnable();
  Thread thread = new Thread(myRunnable);
  myThread.start();
  thread.start();
  }
  }
  A.prints: MyThread: start() followed by MyRunnable: run()
  B.prints: MyThread: run() followed by MyRunnable: start()
  C.prints: MyThread: start() follow

  编辑特别推荐:

  指点一下:到底该不该去考JAVA认证?

  Java认证权威问答精华集

上一页12下一页

视频学习

我考网版权与免责声明

① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;

② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。

最近更新

社区交流

考试问答