Multi-thread programming-two ways to create a thread: two ways to create a thread

Source: Internet
Author: User

Multi-thread programming-two ways to create a thread: two ways to create a thread

1. Purpose

Create a thread to get a thread instance. This thread instance must have methods to control its own life cycle, such as enabling, waiting, and waking.

 

2. Create a Thread

Method 1: new Thread (), new Thread (String name)

1/** 2 * there are also differences between the two constructors: different Thread names 3 */4 public class Thread implements Runnable {5 private char name []; 6 7 // 1. 8/* 9 * Automatically generated names are of the form * "Thread-" + n, where n is an integer10 */11 public Thread () {12 init (null, null, "Thread-" + nextThreadNum (), 0); 13} 14 private static synchronized int nextThreadNum () {15 return threadInitNumber ++; 16} 17 // 2. 18 public Thread (String name ) {19 init (null, null, name, 0); 20} 21 // initialization thread-init ()? 22 private void init (ThreadGroup g, Runnable target, String name, long stackSize) {23 init (g, target, name, stackSize, null ); 24} 25 private void init (ThreadGroup g, Runnable target, String name, long stackSize, AccessControlContext acc) {26 if (name = null) {27 throw new NullPointerException ("name cannot be null"); 28} 29 this. name = name. toCharArray (); 30/** The following is the initialization thread code-omitted */31} 32}

 

Method 2: new Thread (Runnable run), new Thread (Runnable run, String name)

public interface Runnable {

public abstract void run();

}


/** Overwrite and replace the current object with the "external Runnable object" (the purpose is to allow the injection object to have the management Thread of the Thread object) */public class Thread implements Runnable {private Runnable target; public void run () {if (target! = Null) {target. run (); // call the run () method of the "external thread object}// Except for other methods of run (), all objects are Thread. currentThread ()}

 

 

3. Create a custom thread

Method 1: Inherit java. lang. Thread

/**
* 1. constructor class
*/
Public class MyThread extends Thread {// @ Override // whether to overwrite the run () method of the parent class Object public void run () {// do something }}/**
* 2. Create an instance
*/Public class Main {public void static void main (String args) {Thread t = new MyThread ();// Create a Thread --> method 1
T. run (); // t's run () is called
        }}

 

Method 2: implement the Runnable interface

public class MyThread implements Runnable {    public run() {         //do something    }
public class Main{    public void static void main(String args){
             MyThread mt =new MyThread();
Thread t = new Thread (mt );// Create a Thread --> method 2------> Thread. currentThread () => main|This object => t|T <init> called
T. run (); ------> Thread. currentThread () => t | this object => t|MtRun () of is called
/** RemoveOther methods of run () are aimed at Thread. currentThread (), so */
T. start (); ------> Thread. currentThread () => t | this object => t| TThe start () of is called.
T. stop (); ------> Thread. currentThread () => t|This object => t| TThe stop () of is called.

}
}

 

 

4. Resource Sharing

Example: class MyThread --> private int I = 5;

 

Method 1: do not share

 
public class MyThread extends Thread {
private int i =5; public void run() { i--;
while(i>0){
System.out.println(this.currentThread().getName()+":"+i);
} }}


public class Main{ public void static void main(String args){ Thread t1 =new MyThread("t1");
             Thread t2 =new MyThread("t2");
                    t1.start();  
                    t2.start();       
}

}

/** Output:
* T1: 4
* T2: 4
* T1: 3
* T2: 3
*....
*/

 

Method 2: Share

(Implementation of the Runnable interface)

 

Cause:

Private native void start0 ()

Related Article

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.