Java Learning Note (37)-Creating threads that inherit thread

Source: Internet
Author: User

To create a thread that inherits thread
/ * * Create thread Mode one: inherit from thread * Step: * 1. Custom class, inherited from Thread class * 2. Override the Run () method * 3. Create an instance of the class, that is, create a thread * 4. Call the thread's start () method to: Start the thread, call the Run () method * * * thread's Run method, used to store the code to be executed by the thread, must override this method * This method is called automatically when the thread is started * / class Demo extends Thread{@Override Public voidRun () { for(intI=1; i<= -; i++) {System.out.println ("*****run*******"+i); }    }} Public  class Test01 {     Public Static voidMain (string[] args) {Demo demo=NewDemo ();//Create a threadDemo.start ();//Start the thread and execute the thread's Run method        //demo.run (); Just the method that called the object, although the thread was created but did not start         for(intI=1; i<= -; i++) {System.out.println ("*****hello world*******"+i); }    }}
Create Two threads
/ * * Create two threads * * Thread default name * Custom thread: thread-number, starting from 0 * Main thread: main * * View Thread name * GETNAME () * Thread.CurrentThread () get current thread instance * * Set Thread Name * by construction method, * */ class MyThread extends Thread {     PublicMyThread (String name) {Super(name); } @Override Public voidRun () {System.out.println (thread.currentthread () = = This); for(inti =1; I <= -; i++) {//system.out.println (GetName () + "*****run*******" + i);System.out.println (Thread.CurrentThread (). GetName () +"*****run*******"+ i); }    }} Public  class Test02 {     Public Static voidMain (string[] args) {//Create two threads and startMyThread T1 =NewMyThread ("Thread One"); MyThread t2 =NewMyThread ("Thread Two"); T1.setname ("First");        T1.start ();        T2.start (); Thread Thread=thread.currentthread ();//Get current thread instance         for(inti =1; I <= -; i++) {System.out.println (Thread.getname () +"*****hello world*******"+ i); }    }}

Java Learning Note (37)-Creating threads that inherit 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.