Creation and operation of threads

Source: Internet
Author: User

Java provides two ways to create a thread:

    • Inherit the thread class and overwrite the run () method.

    • Create a class that implements the Runnable interface. Use the thread constructor with parameters to create the thread object.

Here we only take the Runnable interface as an example:

package concurrency;public class test1 {    public static  Void main (String[] args)  {        for (int i =  1; i <= 10; i++)  {             calculator calculator = new calculator (i);             thread thread = new thread (Calculator);             thread.start ();         }    }}class calculator implements runnable {     private int number;    public calculator (int  Number)  {        this.number = number;     }     @Override     public void run ()  {         for (int i = 1; i <= 10; i++)  {             system.out.printf ("%s:  %d * %d =  %d\n ",                      thread.currentthread (). GetName (), Number,i,i*number);         }    }}

When the start () method of the thread object is called, another thread of execution is created. Thus, in our program, each time the start () method is called, an execution thread is created.

When all the threads of a program are running, it is more clear that the Java program will end when all non-daemon (Non-daemon) threads are complete. If the initial thread (the thread that executes the main () method) ends, the remaining threads will continue to execute until they run to the end. If a thread calls the System.exit () directive to end the execution of the program, all threads will end.

For a class that implements the Runnable interface, creating a thread object does not create a new execution thread, and similarly, calling its run () method does not create a new thread of execution. A new execution thread is created only when it is called by the start () method.

Writing a class and inheriting the thread class, overwriting the run () method in this class, and then creating an object of that class, and invoking the start () method, also creates an execution thread, as an example:

package concurrency;public class test2 extends thread {     Private int number;    public test2 (Int number)  {         this.number = number;    }     public static void main (String[] args)  {         for (int i = 1; i <= 10; i++)  {             test2 thread = new test2 (i);             thread.start ();         }    }     @Override     public  void run ()  {        for (int i = 1;  i <= 10; i+ +)  {            system.out.printf ("%s:   %d * %d = %d\n ",                      thread.currentthread (). GetName (), Number,i,i*number );         }    }}


Creation and operation of threads

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.