Five internal class forms hide threads in class __ inner class

Source: Internet
Author: User
Tags thread class

1. Using normal inner classes to inherit the thread class

public class Threadtestone {

    private int countdown = 5;
    Private Inner Inner;
    Private class Inner extends thread{

        Inner (String name) {
            super (name);
            Start ();
        }

        public void Run () {while
            (true) {
                System.out.println (this);
                if (--countdown = = 0) return
                    ;
                try{Sleep
                    (ten);
                } catch (Interruptedexception e) {
                    throw new RuntimeException (e);
                }

        }} Public String toString () {return
            "#" +getname () + ":" +countdown
        }
    }

    Public Threadtestone (String name) {
        inner = new Inner (name);
    }

}

2. Use anonymous inner classes to construct the thread class, overriding the run () method

public class Threadtesttwo {

    private int countdown = 5;
    Private Thread t;

    Public Threadtesttwo (String name) {
        T = new Thread (name) {public
            void run () {while
                (true)
                    { System.out.println (this);
                    if (--countdown = = 0) return
                        ;
                    try{Sleep
                        (ten);
                    } catch (Interruptedexception e) {
                        throw new RuntimeException (e);
                    }

            }} Public String toString () {return
                "#" +getname () + ":" +countdown;
            }
        };
        T.start ();
    }

3. Use ordinary inner class to implement Runnable class

public class Runnabletestone {
    private int countdown = 5;
    Private Inner Inner;
    Private class Inner implements runnable{
        Thread t;
        Inner (String name) {
            T = new Thread (this,name);
            T.start ();
        }

        public void Run () {while
            (true) {
                System.out.println (this);
                if (--countdown = = 0) return
                    ;
                try{
                    Thread.Sleep (ten);
                } catch (Interruptedexception e) {
                    throw new RuntimeException (e);
                }

        }} Public String toString () {return
            "#" +thread.currentthread (). GetName () + ":" +countdown
        }
    }

    Public Runnabletestone (String name) {
        inner = new Inner (name);
    }
}

4. Constructs the thread class using an anonymous inner class, parameter 1 constructs a Runnable object, and Parameter 2 indicates the thread name

public class Runnabletesttwo {

    private int countdown = 5;
    Private Thread t;

    Public Runnabletesttwo (String name) {
        T = new Thread (new Runnable () {public

            void run () {while
                (true)
                    { System.out.println (this);
                    if (--countdown = = 0) return
                        ;
                    try{
                        Thread.Sleep (ten);
                    } catch (Interruptedexception e) {
                        throw new RuntimeException (e);
                    }

            }} Public String toString () {return
                "#" +thread.currentthread (). GetName () + ":" +countdown;
            }
        },name);
        T.start ();
    }

5. Use the local inner class, that is, to construct the thread class inside the method, overriding the run () method

public class Threadmethod {private int countdown = 5;
    Private Thread t;

    private String name;
    Public Threadmethod (String name) {this.name = name; The public void Runthread () {if (t = = null) {t = new Thread (name) {public void run (
                        ) {while (true) {System.out.println (this);
                        if (--countdown = = 0) return;
                        try{sleep (10);
                        }catch (Interruptedexception e) {throw new RuntimeException (e); }} public String toString () {return "#" +getnam
                E () + ":" +countdown;
            }
            };
        T.start (); }
    }
}

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.