Exercise of Thread(2)

11.Which three are methods of the Object class?(Choose three.)

A.    notify();
B.    notifyAll();
C.    isInterrupted();
D.    synchronized();
E.    interrupt();
F.    wait(long msecs);
G.    sleep(long msecs);
H.    yield();

(A.B.F).  Java Doc


12.Given the following.

1.public class WaitTest{
2.    public static void main(String []args){
3.        System.out.print("1    ");
4.        synchroized(args){
5.            System.out.print("2    ");
6.            try{
7.                args.wait();
8.                }
9.                catch(InterruptedException e){}
10.        }
11.        System.out.print("3    ");
12.    }
13.}

what is the result of trying of compile and run this program?

A.    It fails to compile because the IllegalMonitorStateException of wait() is not dealt with in line 7.

B.    1 2 3

C.    1 3

D.    1 2

E.    At runtime,it throws an IllegalMonitorStateException when trying to wait.

F.    It will fail to compile because it has to be synchronized on the this object.

(D).将打印1和2,但是,因为没有其他线程通告主线程,他们不会返回调用WAIT,所以3不会被打印,程序会冻结在第7行;    A是错误的,因为IllegalMonitorStateException是一个不检测的异常,所以它不会进行明确的处理;    F是错误的,因为任何OBJECT都可以使用synchroized,此外,当运行静态方法时,它不属于这个对象;


13.Assume the following method is properly synchronized and called from a thread A on an object B:wait(2000);

After calling this method,when will the thread A become a candidate to get another turn at the CPU?

A.    After thread A is notified,or after two seconds.

B.    After the lock on B is released, or after two seconds.

C.    Two seconds after thread A is notified.

D.    Two seconds after lock B is released.


(A).    A是对的,两个事件中的任意的一个,将会变成一个等待中的线程。    B不对,因为一个等待中的线程,除非发生一个notification,否则在释放锁的时候不会返回Runnable。    C是错的,因为线程在notification后,将直接变成一个等待中的线程.    


14.Which two are true?

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.When a thread is waiting as a result of wait(),it release its locks.

E.The notify() method causes a thread to immediately release its locks.

F.The difference between notify() and notifyAll() is that notiffyAll() notifies all waiting threads,regardless of the object they're waiting on.

(A.D). java doc


15.Assume you create a program and one of your threads (called backgroundThread)does some lengthy numerical processing. What would be the proper way of setting its priority to try to get the rest of the system to be very responsive while the thread is running?(Choose all that apply.)

A.backgroundThread.setPriority(Thread.LOW_PRIORITY);

B.backgroundThread.setPriority(Thread.MAX_PRIORITY);

C.backgroundThread.setPriority(1);

D.backgroundThread.setPriority(Thread.NO_PRIORITY);

E.backgroundThread.setPriority(Thread.MIN_PRIORITY);

F.backgroundThread.setPriority(Thread.NORM_PRIORITY);

G.backgroundThread.setPriority(10);

(C.E).  设置线程优先级,最高为10,默认为5。三个方法为:MAX_PRIORITY,MIN_PRIORITY,NORM_PRIORITY。



16.Which three guarantee that a thread will leave the running state?

A.    yield()

B.    wait()

C.    notify()

D.    notifyAll()

E.    sleep(1000)

F.    aLiveThread.join()

G.    Thread.killThread()


(B.E.F).Java doc


17.Which two are true?

A.Deadlock will not occur if wait()/notify() is used.

B.A thread will resume execution as soon as its sleep duration expires.

C.Synchronization can prevent two objects from being accessed by the same thread.

D.The wait() method is overloaded to accept a duration.

E.The notify() method is overloaded to accept a duration.

F.Both wait() and notify() must be called from a synchronized context.

G.wait() can throw a runtime exception.

H.sleep() can throw a runtime exception.


(D.F).


18.Which two are valid constructors for Thread?

A.    Thread(Runnable r,String name)

B.    Thread()

C.    Thread(int priority)

D.    Thread(Runnable r,ThreadGroup g)

E.    Thread(Runnable r,int priority)


(A.B).Java doc



19.Given the following.

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();
     }
}

what is the result?

A.    foo

B.    MyThread foo

C.    MyThread bar

D.    foo bar

E.    foo bar baz

F.    bar foo

G.    Compilation fails.


(B).在main()方法开始后,我们在匿名的内部类里创建了一个实例,并用MyThread来实例化,所以MyThread的构造器将会打印出"MyThread",接下来,程序声明了在main()里用t的实例来调用start()方法,因为run()方法在匿名的内部类里被覆写了,所以会打印"foo"
注:inner class內的程序可像outer classmethod般直接存取outer classfieldsmethods。定义内部匿名类时,需要用";"来结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值