SCJP考试中的线程题目分析

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

  1:
  Which two CANNOT directly cause a thread to stop executing? (Choose Two)
  A.Existing from a synchronized block
  B.Calling the wait method on an object
  C.Calling notify method on an object
  D.Calling read method on an InputStream object
  E.Calling the SetPriority method on a Thread object
  Answer:AC。同55题
  2:
  public class SyncTest{
  public static void main(String[] args) {
  final StringBuffer s1= new StringBuffer();
  final StringBuffer s2= new StringBuffer();
  new Thread ()  {
  public void run() {
  synchronized(s1) {
  s2.append(“A”);
  synchronized(s2) {
  s2.append(“B”);
  System.out.print(s1);
  System.out.print(s2);
  }
  }
  }
  }.start();
  new Thread() {
  public void run() {
  synchronized(s2) {
  s2.append(“C”);
  synchronized(s1) {
  s1.append(“D”);
  System.out.print(s2);
  System.out.print(s1);
  }
  }
  }
  }.start();
  }
  }
  Which two statements are true? (Choose Two)
  A. The program prints “ABBCAD”
  B. The program prints “CDDACB”
  C. The program prints “ADCBADBC”
  D. The output is a non-deterministic point because of a possible deadlock condition
  E. The output is dependent on the threading model of the system the program is running on.
  Answer:DE
  3:
  What will happen when you attempt to compile and run the following code?
  public class Test{
  int i = 0;
  public static void main(String argv[]) {
  Test t = new Test();
  t.myMethod();
  }
  public void myMethod(){
  while(true) {
  try {  wait();
  }catch (InterruptedException e) {}
  i++;
  }
  }
  }
  A. Compile time error, no matching notify within the method.
  B. Compile and run but an infinite looping of the while method.
  C. Compilation and run without any output.
  E. Runtime Exception "IllegalMonitorStatException".
  Answer: E
  Note: The wait/notify protocol can only be used within code that is synchronized. In this case calling code does not have a lock on the object(not synchronized) and will thus cause an Exception at runtime.
  4:
  1.10 What is the result of compiling and executing the following code?
  public class ThreadTest extends Thread {
  public void run() {
  System.out.println("In run");
  yield();  System.out.println("Leaving run");
  }
  public static void main(String args []) {
  (new ThreadTest()).start();
  }
  }
  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.
  E. The code compiles correctly, but nothing is displayed.
  Answer: D

上一页12下一页

视频学习

我考网版权与免责声明

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

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

最近更新

社区交流

考试问答