Java Thread creation method analysis

Source: Internet
Author: User
Tags instance method thread class

Java Thread creation method analysis

The java language has built-in multi-thread support. All classes that implement the Runnable interface can start a new thread. The new thread will execute the run () method of the instance. When run () after the method is executed, the thread ends. Once a thread is executed, the instance cannot be restarted. You can only generate a new instance and start a new thread.

 

1: declare the class as a subclass of Tread. This subclass should overwrite the run method of the Thread class.

Class PrimeTread extends Tread {
PrimeTread (long min ){
This. min = min;
                }
Public void run (){
......
                }
      }
     
Primetread p = new PrimeTread ();
P. start ();
 
2: declare the class that implements the Runnable interface. This class then implements the run method. Then, you can allocate instances of this class and pass and start them as a parameter when creating a Thread.

                
Class PrimeTread implements Runnable {
PrimeTread (long min ){
This. min = min;
           }
Public void run (){
......
           }
      }
     
PrimeTread p = new PrimeTread ();
New Tread (p). start ();
      
The Thread class is an instance that implements the Runnable interface and represents a Thread instance. The only way to start a Thread is through the start () instance method of the Thread class:

Thread t = new Thread ();
T. start ()

The start () method is a native method that starts a new thread and runs the run () method. The default run () method of the Thread class exits without doing anything. Note: directly calling the run () method does not start a new thread. It is no different from calling a common java method.

Therefore, there are two ways to implement your own thread:

Method 1: Your own extend Thread class, and re-write the run () method, you can start a new Thread and execute your own defined run () method. For example:

Public class MyThread extends Thread {
Public run (){
System. out. println ("MyThread. run ()");
     }
}

Start the thread in the appropriate place: new MyThread (). start ();

Method 2: If your class has extends another class, you cannot directly extends Thread. At this time, you must implement a Runnable interface:

Public class MyThread extends OtherClass implements Runnable {
Public run (){
System. out. println ("MyThread. run ()");
     }
}

To start MyThread, you must first instantiate a Thread and input your own MyThread instance:

MyThread myt = new MyThread ();
Thread t = new Thread (myt );
T. start ();

In fact, after passing in a Runnable target parameter to the Thread, the Thread's run () method will call target. run (). Refer to the JDK source code:

Public void run (){
If (target! = Null ){
Target. run ();
     }
}

The thread has some other settings, such as Name, ThreadGroup, and isDaemon, which are rarely associated with the thread design mode.

Thread synchronization

 

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.