zoukankan      html  css  js  c++  java
  • 五种内部类形式将线程隐藏于类中

    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();
            }
        }
    }
    
  • 相关阅读:
    [转]html5 Canvas画图教程(6)—canvas里画曲线之arcTo方法
    [转]html5 Canvas画图教程(5)—canvas里画曲线之arc方法
    [转]html5 Canvas画图4:填充和渐变
    [转]html5 Canvas画图教程(3)—canvas出现1像素线条模糊不清的原因
    [转]html5 Canvas画图教程(2)—画直线与设置线条的样式如颜色/端点/交汇点
    【Java进阶】Maven 简介
    【Java入地】Spring 的 作用域 & MVC & SSM
    【Linux】Kali 物理机安装笔记、简单过程及注意事项
    【电脑维修】 显卡错误(错误代码:43)
    【Linux】 编程环境搭建 JDK、Node.js、Npm、Yarn、Redis、Maven的安装与搭建
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/8335974.html
Copyright © 2011-2022 走看看