Five internal class forms to hide threads in a class

Source: Internet
Author: User

1. Inheriting the thread class with a normal inner class

 Public  class threadtestone {    Private intCountdown =5;PrivateInner 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 NewRuntimeException (e); }            }        } PublicStringtoString(){return "#"+getname () +":"+countdown; }    } Public Threadtestone(String name) {inner =NewInner (name); }}

2. Construct the thread class with an anonymous inner class, overriding the run () method

 Public classThreadtesttwo {Private intCountdown =5;PrivateThread T; Public Threadtesttwo(String name) {T =NewThread (name) { Public void Run(){ while(true) {System. out. println ( This);if(--countdown = =0)return;Try{Sleep (Ten); }Catch(Interruptedexception e) {Throw NewRuntimeException (e); }                }            } PublicStringtoString(){return "#"+getname () +":"+countdown;        }        };    T.start (); }}

3. Implementing the Runnable class with ordinary inner classes

 Public  class runnabletestone {    Private intCountdown =5;PrivateInner Inner;Private  class Inner implements Runnable{Thread T; Inner (String name) {T =NewThread ( 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 NewRuntimeException (e); }            }        } PublicStringtoString(){return "#"+thread.currentthread (). GetName () +":"+countdown; }    } Public Runnabletestone(String name) {inner =NewInner (name); }}

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

 Public classRunnabletesttwo {Private intCountdown =5;PrivateThread T; Public Runnabletesttwo(String name) {T =NewThread (NewRunnable () { Public void Run(){ while(true) {System. out. println ( This);if(--countdown = =0)return;Try{Thread.Sleep (Ten); }Catch(Interruptedexception e) {Throw NewRuntimeException (e); }                }            } PublicStringtoString(){return "#"+thread.currentthread (). GetName () +":"+countdown;        }},name);    T.start (); }}

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

 Public classThreadmethod {Private intCountdown =5;PrivateThread T;PrivateString name; Public Threadmethod(String name) { This. name = name; } Public void Runthread(){if(T = =NULL) {T =NewThread (name) { Public void Run(){ while(true) {System. out. println ( This);if(--countdown = =0)return;Try{Sleep (Ten); }Catch(Interruptedexception e) {Throw NewRuntimeException (e); }                    }                } PublicStringtoString(){return "#"+getname () +":"+countdown;            }            };        T.start (); }    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Five internal class forms to hide threads in a 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.