Exercise of thread (3)

Source: Internet
Author: User

20. Given the following.

Public class synctestinner {
Public static void main (string [] ARGs ){
Thread t = new thread (){
Foo F = new Foo ();{
F. Increase (20 );
}
};
T. Start ();
}
}

And assuming that data must be protected from completion uption, what-if anything-can you add to the preceding code to ensure the integrity of data?

A. Synchronize the run method.

B. Warp A synchronize (this) around the call to F. Increase ().

C. The existing code will not compile.

D. The existing code will cause a runtime exception.

E. Put in a wait () call prior to invoking the increase () method.

F. Synchronize the increase () method.

(F ).

21. Given the following.

1. Public class test {
2. Public static void main (string [] ARGs ){
3. Final Foo F = new Foo ();
4. thread t = new thread (New runnable ()){
5. Public void run (){
6. f. dostuff ();
7 .}
8 .}};
9. Thread G = new thread (){
10. Public void run (){
11. f. dostuff ();
12 .}
13 .};
14. T. Start ();
15. G. Start ();
16 .}
17 .}

1. Class Foo {
2. Int x = 5;
3. Public void dostuff (){
4. If (x <10 ){
5. // nothing to do
6. Try {
7. Wait ();
8.} catch (interruptedexception ex ){}
9.} else (){
10. system. Out. println ("X is" + x ++ );
11. If (x> = 10 ){
12. Notify ();
13 .}
14 .}
15 .}
16 .}

What is the result?

A. The Code will not compile because of an error on line 12 of class Foo.

B. The Code will not compile because of an error on line 7 of class Foo.

C. The Code will not compile because of an error on line 12 of class test.

D. The Code will not compile because of some other error in class test.

E. An exception occurs at runtime.

F. X is 5
X is 6

(E). Because the thread does not belong to the object lock when calling wait (), if the method is synchronous, the code will encounter an exception during runtime.

Public static void main (string [] ARGs)
{
New thread ()
{
Public void run () {system. Out. println ("Run ");}
}. Start ();
}
This code will not generate compile Error !!

Public Class
{
Private interface Ina
{
Void show ();
}
/*
Class CIA implements Ina
{
Void show ()
{
// Some great code here
}
}
*/
}


If the interface ina does not want to give other class implement, Mark private here.
However, after ina mark private is set, only the inner class of A can be implement.
Therefore, in this case, Ina does not actually play a major role. It is just a legal syntax.

Boolean A = Boolean. True;

This line of code is incorrect, because Boolean. True is a Boolean !! Not Boolean !!

Class base extends thread
{
Static public void main (string [])
{
Base anobj = new base ();
Anobj. Start ();
}
}

Compilation will not go wrong, because the Thread class implements runnable interface

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.