Exercise of thread (2)

Source: Internet
Author: User

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

A. Notify ();
B. policyall ();
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 and 2 will be printed. However, because no other thread notifies the main thread, they will not return to call wait, SO 3 will not be printed, and the program will be frozen in 7th rows; A is wrong, because illegalmonitorstateexception is an undetected exception, it will not be explicitly processed; F is incorrect, because any object can use synchroized. In addition, when running static methods, it does not belong to this object;

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 is correct. Any one of the two events will become a thread waiting. B is incorrect, because a waiting thread does not return runnable when the lock is released unless a notification occurs. C is wrong, because the thread will directly become a waiting thread after notification.

14. Which two are true?

A. The policyall () method must be called from a synchronized context.

B. To call wait (), an object must own the lock on the thread.

C. The specified y () 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 specified y () method causes a thread to immediately release its locks.

F. The difference between Y () and policyall () Is that nopolicyall () 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 wocould 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). Set the thread priority. The maximum value is 10. The default value is 5. The three methods are max_priority, min_priority, and norm_priority.

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

A. Yield ()

B. Wait ()

C. Sort y ()

D. policyall ()

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 ()/Policy () 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 specified y () method is overloaded to accept a duration.

F. Both wait () and Policy () 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 ). after the main () method starts, an instance is created in the anonymous internal class and instantiated using mythread. Therefore, the mythread constructor will print "mythread". Next, the program declares that the START () method is called using the T instance in main (), because the run () method is overwritten in the anonymous internal class, so "foo" is printed"
Note: Programs in inner class can directly access fields and methods of outer class as the method of outer class. When defining an internal Anonymous class, you need to end.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.