java第九章练习题(多线程机制)

第九章练习题(多线程机制)

 

    1.线程由以下哪些部分组成?(选三项)

    A.虚拟的CPU

    B.CPu执行的代码

    C.支持代码工作的数据

    D.进程

    

    2.创建线程的时候必须实现哪项接口?

    A.  Runnable

    B.  Thread

    C.  Run

    D.  Start

    

    3.启动一个线程的方法是哪项?

    A. join()

    B.  run f)

    C.start()

    D.sleep()

    

    4.当一个处于阻塞状态的线程解除阻塞后,它将回到哪个状态?

    A.运行中状态

    B.结束状态

    C.新建状态

    D.可运行状态

    

     5.可以通过继承哪个类来创建线程?

    A. Thread

    B.Runnable

    C.tart

    D.run

、    6.为了保证方法的线程安全,声明方法的时候必须用哪个修饰符?

    A.new

    B. transient

    C. void

    D. synchronized

    

    7.线程的默认优先级是哪项?

    A.  0

    B.  1

    c.   5

    D.  10

 

    8.现有:

    1. class Thread2 implements Runnable  (

    2.    void run()  {  //public void run

    3.System.out.print ("go¨);

    4.    }

    5.

    6.public static void main(String  []  args)  {    .

    7.Thread2 t2=new Thread2();

    8.Thread t=new Thread(t2);

    9.    t.start();

    10.    }

    11.  }

    结果为:

    A. go

    B.运行时异常被抛出

    c.代码运行,无输出结果

    D.编译失败

  

    9.程序员想要创建一个名为MyThread的类以便在main方法中用Thread实例

    化。对于下面三行:

    MyThread必须继承Thread。

    MyThread必须实现Thread。

    MyThread必须覆盖public void run()。

    有几行是正确的7

.  A.  0

    B.  1

    C.  2

    D. 3

 

    10.现有:

    1.  class ThreadBoth extends Threaa implements Runnable  {

    2.

    3.public void run()  (System.out.print("hi");  }

    4.

    5.public static voicl main (String  []  args)  {

    6.Thread tl=new ThreadBoth():

    7.Thread t2 = new Thread (tl):

    8.    tl.run():

    9.    t2.run():

    10.    }

    11.  }

    结果为:

     A. hi hi

     B. hi

     C.编译失败

     D.运行时异常被抛出

    

    11.现有:t是一个合法的Thread对象的引用,并且t的合法run()方法如下:

    9.  public void run()  {

    10.    System.out.print ("go");

    11.    }

    及:

    18.  t.start();

    19.  t.start();

    1 n    t.run();

    哪一个是结果?

    A. go go

    B. go go go

    C. go之后跟着一个异常

    D.go go之后跟着一个异常

    12.现有:

1. class ThreadExcept implements Runnable  {

2.    public void run()  {  throw new RuntimeException("exception ");  }

3.

4.    public static void main(Stri_ng  []  args)  {

5.    new  Thread (new  ThreadExcept()).start();

6.    try  {

7.     int x=Integer.parselnt (args [0]);

8.    Thread. sleep (x);

9.     System.out.print("main");

10.    } catch (Exception e)  {  }

11.    }

12.  }

   和命令行:  java ThreadExcept l000

   哪一个是结果?

   A.main

   B.编译失败

   C. main java.lang.RuntimeException: exception

   D.代码运行,但没有输出

 

   13.现有:

   5.  class Order2 implements Runnable  {

   6.    public void run()  {

   7.for (int x- o;  x<4;  x++)  {

   8.try{Thread.sleep(100);  )catch  (Exception e)  {  }

   9.    System.out.print("r");

   10.    }  }

   11.    public static void main(string  []  args)  {

   12.    Thread t=new Thread(new order2());

   13.    t.start();

   14.    for(int x=0;  x<4;  x++)  {

   15.    //insert code here

   16.    System.out.print("m");

   17.  }  }  }

    哪一个插入到第15行,最有可能产生输出 rmrmrmrm?

    A.  Thread.sleep(1);

    B.  Thread.sleep(100);

    C.  Thread.sleep(1000);

    D.  try{  Thread.sleep(1);  )  catch  (Exception e)  {  }

    E.  try{Thread.sleep(100);  )  catch  (Exception  e)  {  }

    F.  try{Thread.sleep(1000);  )catch  (Exception  e)  {  )

    14.现有:

’   1.  class Waiting implements Runnable  {

     2.    boolean flag=false;

     3.public  synchronized void run()  {

     4.    if  (flag)  {

     5.    flag=false;

     6.System.out.print ("1");

     7.    try  {  this.wait();  )  catch  (Exception e)  {  }

     8.System.out.print ("2");

     9.    }

     10.    else  {

     11.    flag=true;

     12.    System.out.print ("3");

     13.    try{Thread.sleep (2000); } catch(Exception e)  {}

     14.    System.out.print ("4");

     15.    notify();

     16.    }

     17.    }

     18.    public static void main (String  []  args)  {

     19.    Waiting w=new Waiting();

     20.    new Thread (w) .start();

     21.    new Thread (w) .start();

     22.    }

     23.  }

     以下哪两项是止确的?(选两项)

     A.代码输出l 3 4

     B.代码输出3 4 1

     C.代码输出l 2 3 4

     D。代码输出1 3 4 2

     E.代码运行完毕

     F.代码不会完成

  5. class Order implements Runnable {

  6. public void run ()  {

  7. try { Thread.sleep (2000) ;  } catch (Exception e)

  8. System.out.print("in") ;

  10. public static void main (String [] args)  {

  11. Thread t = new Thread (new Order ()) ;

  12. t.start () ;

  13. System.out.print ("pre ") ;

  14. try { t.join () ;  } catch (Exception e)  { }

  15. System.out.print ("post") ;

  可产生哪两项结果? (选两项)

  A. pre in post

  B. pre in

  C. in post pre

  D. in pre post

  E. pre post in

  

     参考答案

     1ABD

     2A

     3C

     4D

     SA

     6D

     7C

     8D

     9A

     10 A

     11  C

     12 C

     13 E

     14 B F

     15 A D

猜你喜欢

转载自blog.csdn.net/bifuguo/article/details/82886886