How do I create and start a thread?

Source: Internet
Author: User
Tags thread class

Translated from: http://www.tqcto.com/article/recommend/137.html

One, define the thread
1, inherit the Java.lang.Thread class.
There is a run () method in this class, and you should be aware of its usage:
public void Run ()

If the thread is constructed using a standalone Runnable run object, the Run method of the Runnable object is called, otherwise the method does nothing and returns.
Subclasses of Thread should override this method.


2, realize java.lang.Runnable interface.
public void Run ()

When you create a thread using an object that implements the interface Runnable, starting the thread causes the Run method of the object to be called in the thread that executes independently.
The general contract for the method run is that it may perform any required operations.

Second, instantiate the thread

1, if the extension of the Java.lang.Thread class thread, then the direct new can.

2, if the implementation of the Java.lang.Runnable interface class, then use the thread construction method:
Java code thread (Runnable target) thread (Runnable target, String name) thread (threadgroup Group, Runnable target) T Hread (threadgroup group, Runnable Target, string name) Thread (threadgroup Group, Runnable Target, string name, long STA Cksize)


Third, start the thread

Call the Start () method on the thread object of the thread, not run () or any other method.

Before the start () method is called: The thread is in the new state, the new state refers to a thread object, but there is no real thread.

After calling the start () method: A series of complex things happened
Start a new execution thread (with a new call stack);
The thread is transferred from the new state to the operational state;
When the thread obtains an opportunity to execute, its target run () method runs.

Note: For Java, the run () method has nothing special. Like the main () method, it is just a new thread that knows the method name (and signature) of the call. Therefore, it is legal to raise the Run method on runnable or thread. But does not start a new thread.


Iv. examples

1. Multithreading example of implementing Runnable interface
Java code  /**   *  implements runnable interface classes    *   *  @author  leizhimin  2008-9-13 18:12:10   */    public class dosomething implements  Runnable {        private String name;            public dosomething (string name)  {            this.name = name;         }           public void run ()  {             for  (int i = 0; i  < 5; i++)  {                 for  (long k = 0; k < 100000000; k++)  ;     &NBSP;&NBSP;&NBsp;         system.out.println (name +  ": "  +  i);            }         }   }      /**   *  Testing multi-threaded routines implemented by runnable classes     *   *  @author  leizhimin 2008-9-13 18:15:02   */    public class testrunnable {        public static  Void main (String[] args)  {             Dosomething ds1 = new dosomething ("Ah san");             dosomething ds2 = new dosomething ("John Doe");               thread t1 = new thread (DS1);     &NBSP;        thread t2 = new thread (DS2);               t1.start ();             t2.start ();        }   }   


Execution Result:  
John Doe: 0 
A III: 0&NBSP;
John Doe: 1 
A III: 1&NBSP;
John Doe: 2 
John Doe: 3 
A: 2  
John Doe: 4 
A III: 3&NBSP;
A III: 4&NBSP;

Process finished with exit code 0 

2, Extending the thread class implementation of multithreaded example  

Java code  /**   *  Test extension thread class implementation of multi-threaded programming    *   *   @author  leizhimin 2008-9-13 18:22:13   */    Public class  TestThread extends Thread{        public testthread ( String name)  {            super (name);         }           public void  run ()  {            for (int i =  0;i<5;i++) {   

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.