Java section 54th inherits the thread class to create threads

Source: Internet
Author: User

2016-07-01

1 inheriting the thread class creating threads
Class Name extends thread{
public void Runa () {
Related code
}
}

 Packagecom.java1995;/*** Inherit thread class to create threads *@authorAdministrator **/ Public classMyThreadextendsthread{//constructors: Naming Threads     PublicMyThread (String name) {Super(name); }    //override the Run () method     Public voidrun () { for(inti=0;i<10;i++){            Try{Thread.Sleep (1000); }Catch(interruptedexception e) {e.printstacktrace (); }//1000 msSystem.out.println ("MyThread" +i); }        }}

 Packagecom.java1995; Public classThreaddemo { Public Static voidMain (string[] args) {MyThread Mt=NewMyThread ("Thread 1"); MyThread mt2=NewMyThread ("Thread 2"); Mt.start ();//MultithreadingMt2.start (); //Mt.run ();//Single Thread         for(inti=0;i<10;i++){            Try{Thread.Sleep (1000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("Main thread" +i); } System.out.println ("Program execution Complete"); }}

Resources

[1] Java Easy Start Classic tutorial "full version"

Java section 54th inherits the thread class to create 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.