SCJP考试中的线程题目分析 |
来源:www.chinajavaworld.net |
|
|
Which two CANNOT dir
| ectly 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 metho
| d on an InputStream object
|
E.Calling the SetPri
| ority method on a Thread obj
| ect
|
public static void main(String[
| ] args) {
|
final StringBuffer s1= new Stri
| ngBuffer();
|
final StringBuffer
| s2= new StringBuffer();
|
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 n condition
| on-deterministic point becau
| se of a possible deadlock
|
E. The output is dep is running on.
| endent on the threading mode
| l of the system the program
|
What will happen whe
| n you attempt to compile and
| run the following code?
|
public static void main(Strin
| g argv[]) {
|
}catch (InterruptedException e
| ) {}
|
A. Compile time erro
| r, no matching notify within
| the method.
|
B. Compile and run b
| ut an infinite looping of th
| e while method.
|
C. Compilation and run without a
| ny output.
|
E. Runtime Exception
| "IllegalMonitorStatExceptio
| n".
|
Note: The wait/notif synchronized. In this ca synchronized) and will t
| y protocol can only be used se calling code does not hav hus cause an Exception at ru
| within code that is e a lock on the object(not ntime.
|
1.10 What is the result of compi
| ling and executing the following code?
|
public class ThreadT
| est extends Thread {
|
System.out.println("In run"); |
System.out.println("Leaving run"); |
public static void main(String
| args []) {
|
(new ThreadTest()).start(); |
A. The code fails to
| compile in the main() metho
| d.
|
B. The code fails to
| compile in the run() method
| .
|
C. Only the text "In run" will b
| e displayed.
|
D. The text "In run"
| followed by "Leaving run" w
| ill be displayed.
|
E. The code compiles correctly,
| but nothing is displayed.
|
1.12 Which of the following will wait()B. notify()C. yield()D. suspen
| definitely stop a thread from executing?A. d()E. sleep()Answer: ACDE
|
1.12 Which of the fo
| llowing will definitely stop
| a thread from executing?
|
Which of the follow
| ing statements about threadi
| ng are true?
|
A. You can only obtain a mutuall extends Thread or implements runnabl
| y exclusive lock on methods in a class that e.
|
B. You can obtain a mutually exc
| lusive lock on any object.
|
C. You can't obtain
| a mutually exclusive lock on
| any object.
|
D. Thread scheduling
| algorithms are platform dep
| endent.
|
Consider the following statement: |
Thread myThread = new Thread(); |
Which of the followi
| ng statements are true regar
| ding myThread?
|
A. The thread myThre
| ad is now in a runnable stat
| e.
|
B. The thread myThre
| ad has a priority of 5.
|
C. On calling the start() method class will be executed.
| on myThread, the run method in the Thread
|
D. On calling the st class will be executed.
| art() method on myThread, th
| e run method in the calling
|
Note: the priority o the constructor.
| f myThread will be inherited
| from the Thread that called
|
What is the effect of issuing a
| wait() method on an object?(Mutiple)
|
1) If a notify() met effect
| hod has already been sent to
| that object then it has no
|
2) The object issuing the call t a notify() or notifyAll() method
| o wait() will halt until another object sends
|
3) An exception will be raised |
4) The object issuin with any other objects u
| g the call to wait() will be sing the receiving object.
| automatically synchronized
|
Pick all the true statements below. |
1) If a thread wants object's lock.
| to call wait() on an object
| , the thread must own that
|
2) There is a method that you ca that puts the instance to sleep for
| n call on an instance of the Thread class a specified number of milliseconds.
|
3) At the moment whe the object for which it
| n a thread is notified, it a was waiting.
| utomatically gets the lock of
|
2. public static void main(Str
| ing[] args) {
|
9. synchronized(alias1) { |
12. System.out.println("
| DONE WAITING");
|
14. catch (InterruptedException e) { |
15. System.o
| ut.println("INTERRUPTED");
|
17. catch (Exception e) { |
18. System.out.println("
| OTHER EXCEPTION");
|
21. System.out.println("FINALLY"); |
24. System.out.println("ALL DONE"); |
2) The application compiles but
| doesn't print anything.
|
3) The application prints "DONE
| WAITING".
|
4) The application prints "INTER
| RUPTED".
|
5) The application prints "OTHER
| EXCEPTION".
|
What will happen wh
| en you attempt to compile an
| d run the following code?
|
class MyThread extends Thread{ |
System.out.println
| ("MyThread: run()");
|
public void start(){////注意,
| 此方法重写后,不再有CALL RUN()方法的功能。
|
System.out.println("MyThread:
| start()");
|
class MyRunnable implements Runnable{ |
System.out.println("MyRunnable
| : run()");
|
public void start(){//Runnable 否则,将CALL THREAD 的start()执行run
| 并无此方法,所以,除非本类的对象可以CALL 它, ()
|
System.out.println
| ("MyRunnable: start()");
|
public static void main(String
| args[]){
|
MyThread myThread = new MyThread(); |
MyRunnable myRunnable = new My
| Runnable();
|
Thread thread =
| new Thread(myRunnable);
|
A.prints: MyThread: start() foll
| owed by MyRunnable: run()
|
B.prints: MyThread:
| run() followed by MyRunnable
| : start()
|
C.prints: MyThread: start() foll
| owed by MyRunnable: start()
|
D.prints: MyThread: run() follow
| ed by MyRunnable: run()
|
Multiple objects of multiple Threads to crea use the following code?
| MyClass (given below) are us te new integer count. What w D
| ed in a program that uses ill happen when other threads
|
static private int myCount = 0; |
private static synchronized int
| nextCount(){
|
return ++myCount; //myCount为static |
public void getYourNumber(){ |
yourNumber = nextCount(); |
A. the code ill give ompilation error |
B. the code ill give runtime error |
C. each thread will get a unique number |
D. the uniqueness of the number
| different Threads can’t be guaranteed.
|
What is the effect o
| f issuing a wait() method on
| an object
|
1) If a notify() method has alre effect
| ady been sent to that object then it has no
|
2) The object issuin a notify() or notifyAll(
| g the call to wait() will ha ) method
| lt until another object sends
|
3) An exception will be raised |
4) The object issuing the call t with any other objects using the rec
| o wait() will be automatically synchronized eiving object.
|
|
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理