To enable a new thread

Source: Internet
Author: User

public class TestThread1 {public static void main (string[] args) {Runner1 r = new Runner1 (); <span style= "WHITE-SPACE:PR E "></span>//r.run ();//This place does not belong to the Enable new thread, which belongs to the method call thread t = new Thread (r); T.start ();//When a new thread is enabled, You must use the Start method under thread for (int i=0;i<100;i++) {System.out.println ("Main:" + i);}}} Class Runner1 implements Runnable {public void run () {for (int i=0;i<100;i++) {System.out.println ("Runner1:" + i);}}} /* In the JDK document, the method Start () of the class thread is described as follows: public void start () causes the thread to execute; The Java virtual machine calls the thread's Run method. The result is two threads running concurrently, when the front thread (from the call is returned to the Start method) and another thread (executing its Run method). It is illegal to start a thread multiple times. This is especially the time when the thread has finished executing and cannot be restarted. */

Another way to enable a new thread is to inherit the thread class:

public class TestThread1 {public static void main (string[] args) {Runner1 r = new Runner1 (); R.start (); <span style= "whit E-space:pre "></span>//is inherited from the thread class, so you can also use the inherited Start () method//<span style=" White-space:pre ">< /span>r.run ();//This place does not belong to the Enable new thread, which belongs to the method call//thread t = new Thread (r),//t.start ();//When a new thread is enabled, it must be used with the Start method under thread for ( int i=0;i<100;i++) {System.out.println ("Main:" + i);}}} Class Runner1 extends Thread{//class Runner1 implements Runnable {public void run () {for (int i=0;i<100;i++) {System.out . println ("Runner1:" + i);}}}
Inheritance is not recommended, because inheritance can only be single-inheritance, but the interface can implement multiple inheritance, so in Java, the interface can be implemented as far as possible, do not inherit

To enable a new thread

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.