Multi-threaded < one > created v mode

Source: Internet
Author: User
Tags instance method thread class throwable

One, multithreading is created by inheriting the thread class to create threads

The thread class essentially implements an instance of the Runnable interface, representing an instance of a thread. The only way to start a thread is through the start () instance method of the thread class. The start () method is a native method that starts a new thread and executes the run () method. This way of implementing multithreading is simple, by extend thread directly through your own class, and by copying the run () method, you can start a new thread and execute your own defined run () method. For example:

1  Public classMyThreadextendsThread {2      Public voidrun () {3System.out.println ("Mythread.run ()"); 4 }5 }  6  7MyThread MyThread1 =NewMyThread (); 8MyThread myThread2 =NewMyThread (); 9 Mythread1.start (); TenMythread2.start ();

The main principle of this approach is to inherit the tread class, when

Let's take a look at the source code. When the Start method is called, the Start0 method is called, and start0 has a native keyword, which we can understand to call the Run method when the start0 is called.

1   Public synchronized voidStart ()2     {3         BooleanFlag;4         if(Threadstatus! = 0)5             Throw Newillegalthreadstateexception ();6Group.add ( This);7Flag =false;8 start0 ();9Flag =true;Ten         Try One         { A             if(!flag) -Group.threadstartfailed ( This); -         } the         Catch(Throwable throwable) {} -          Breakmissing_block_label_70; - Exception Exception; - exception; +         Try -         { +             if(!flag) AGroup.threadstartfailed ( This); at         } -         Catch(Throwable throwable1) {} -         Throwexception; -     } -  -     Private native voidStart0 ();
2. Implement runnable Interface Creation thread

If your class already extends another class, you cannot directly extends the Thread, at which point you can implement a runnable interface, as follows:

1  Public class extends Implements    Runnable {  2publicvoid  run () {  3 System.out.println ("Mythread.run ()");   4 }  5 }     

In order to start mythread, you need to instantiate a thread first and pass in your own Mythread instance:

1 New MyThread ();   2 New Thread (myThread);   3 Thread.Start ()

In fact, when a runnable target parameter is passed to thread, the thread's Run () method calls Target.run (), referencing the JDK source code:

1  Public void run () {  2ifnull) {  3  target.run ();   4 }  5 }     

Here a typical fallback function is applied, a call to B,b in call a, in defining an interface definition criterion, to determine the specification.

third, anonymous function creation

The anonymous function must inherit the parent class or implement the interface.

Sometimes we just use it once, so we can use an anonymous function.

 1  new  Thread () {//  creation mode 1  2  public  void   run () { 3  for  (int< /span> x=0; x<50; X++ 4  System.out.println (Thr Ead.currentthread (). GetName () + ".... x=" +x);  5  }  6  }  7 }.start (); 
1 New Thread (new Runnable () {// Create mode 22public     void  run () {     3          for (int x=0; x<50; x + +) {        4             System.out.println (Thread.CurrentThread (). GetName () + ".... z=" +x); 5         }6    }7 }). Start ();

Multi-threaded < one > created v mode

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.