Create multithreaded mode One (inherit the thread class)

Source: Internet
Author: User

Step: 1. Define a class to inherit the thread. 2. Rewrite the Run method. 3. Create the subclass object, which is the thread object. 4. Call the Start method. The thread is opened and the thread executes, and the JVM is told to call the Run method. (Start does two things, open the thread and call the Run method.) )
  • The thread object calls the Run method and calls the Start method difference (interview frequently):
    1. Calling the Run method does not open the thread. The method is called only by the object.
    2. Call start to open the thread and have the JVM call the Run method to execute in the open thread.
Instance:
classDemoextendsthread{PrivateString name; Demo (String name) { This. Name =name;}  Public voidrun () {Show ();} Public voidShow () { for(inti = 0; I < 20; i++) {System.out.println (name+ "..." +i); }}} Public classThreaddemo { Public Static voidMain (string[] args) {Demo D1=NewDemo ("Xiaoqing"); Demo D2=NewDemo ("Wangcai");  D2.start (); D1.run (); //Thread is numbered starting at 0, where two thread objects are created, but the first thread is not turned on.  }}

Create multithreaded mode One (inherit the thread class)

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.