五種內部類形式將線程隱藏於類中__內部類

來源:互聯網
上載者:User

1.使用普通內部類繼承Thread類

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(10);                }catch(InterruptedException e){                    throw new RuntimeException(e);                }            }        }        public String toString(){            return "#"+getName()+":"+countDown;        }    }    public ThreadTestOne(String name){        inner = new Inner(name);    }}

2.使用匿名內部類構造Thread類,重寫run()方法

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(10);                    }catch(InterruptedException e){                        throw new RuntimeException(e);                    }                }            }            public String toString(){                return "#"+getName()+":"+countDown;            }        };        t.start();    }}

3.使用普通內部類實現Runnable類

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(10);                }catch(InterruptedException e){                    throw new RuntimeException(e);                }            }        }        public String toString(){            return "#"+Thread.currentThread().getName()+":"+countDown;        }    }    public RunnableTestOne(String name){        inner = new Inner(name);    }}

4.使用匿名內部類構造Thread類,參數1構造一個Runnable對象,參數2指明線程名稱

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(10);                    }catch(InterruptedException e){                        throw new RuntimeException(e);                    }                }            }            public String toString(){                return "#"+Thread.currentThread().getName()+":"+countDown;            }        },name);        t.start();    }}

5.使用局部內部類,即在方法內部構造Thread類,重寫run()方法

public class ThreadMethod {    private int countDown = 5;    private Thread t;    private String name;    public ThreadMethod(String name){        this.name = name;    }    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 "#"+getName()+":"+countDown;                }            };            t.start();        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.