The Java multithreaded thread is created using anonymous inner classes in two ways

Source: Internet
Author: User
Tags thread class

the anonymous inner class implements two ways of threading:

The first way:

    • 1. Inheriting the thread class
    • 2. Overriding the Run method
    • 3. Write the code that will be executed in the Run method

The second way:

    • 1. How to pass a subclass object of runnable to thread
    • 2. Overriding the Run method
    • 3. Write the executed code in the Run method, and finally we open the thread
 PackageCom.yyx.thread;/*** Anonymous inner class create thread Yyx February 4, 2018*/ Public classAnonymousthread { Public Static voidMain (string[] args) {//inheriting the thread class for multithreading        NewThread () { Public voidrun () { for(intx = 0; x < 50; X + +) {System.out.println (Thread.CurrentThread (). GetName ()+ "--"                            +x);        }}}.start (); //implement runnable excuses, create multithreading and start        NewThread (NewRunnable () {@Override Public voidrun () { for(intx = 0; x < 50; X + +) {System.out.println (Thread.CurrentThread (). GetName ()+ "--"                            +x);    }}}) {}.start (); }}

The Java multithreaded thread is created using anonymous inner classes in two ways

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.