"Multi-threaded learning record One (1)" Creating Java Multithreading

Source: Internet
Author: User

1) There are three ways to create Java multi-threading:

1. Inheriting the thread class

2. Implement the Runnable interface

3. Implement the Callable interface

The first type:

1        //inherit the thread class2     classExtendsthreadextendsthread{3          Public voidrun () {4             //Do your stuff5         }6     }7Extendsthread e =NewExtendsthread ();8E.start ();
View Code

The second type:

1          //implementing the Runnable interface2     classImplerunnablethreadImplementsrunnable{3 @Override4          Public voidrun () {5             //Do your stuff6         }7     }8Implerunnablethread IR =NewImplerunnablethread ();9Thread T =NewThread (IR);TenT.start ();
View Code

The third type is provided after jdk1.5:

1          //implementing the Callable Interface2     classImplecallablethreadImplementscallable{3 @Override4          PublicObject Call ()throwsException {5             //TODO do stuff6             return NULL;7         }8     }9Implecallablethread IC =NewImplecallablethread ();Tenfuturetask<string> task =NewFuturetask<string>(IC); One     NewThread (Task, "Threads with a return value"). Start ();
View Code

2) You can see that the thread starts with the Start method, why? Open Start () source can be seen, in the Thread class start () called the Private native void Start0 (); The native keyword means that the underlying function of the operating system is transferred, and such technology becomes JNI (Java native Interface).

Look at the execution of the thread:

 1  for  ( int  i =0;i<5;i++) { 2  Thread thread = new  Thread ("+i) {  3  public  void   run () { 4  System . OUT.PRINTLN ("Run ...." + GetName ());  5   6  };  7   Thread.Start ();  8 } 
View Code

Output

    

3) Although the order in which threads are started is sequential, the order in which threads are executed is not ordered, because threads are executed in parallel rather than sequentially, and the order in which they are executed is determined by the JVM and the operating system.

4) If you use both the inherited thread class and the Runnable interface, which run method is executed?

1 NewThread (NewRunnable () {2 @Override3              Public voidrun () {4                 //Do your stuff 15             }6         }){7              Public voidrun () {8                 //Do your stuff 29             }Ten};
View Code

The above code is equivalent to the new thread (new Runnable () {run ()}) {Run ()}, because the Thread class itself implements the Runnable interface, so if you override the Run method, it overrides the parent class method. So a second run method is executed.

The next article explains the differences between the three ways to create.

"Multi-threaded learning record One (1)" Creating Java Multithreading

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.