• java多线程基本概述(十四)——Thread内部类的几种写法


    import java.util.concurrent.TimeUnit;
    
    //命名匿名类-Thread
    class InnerThread1 {
        private int countDown = 5;
        private Inner inner;
        private class Inner extends Thread{
            Inner(String name){
                super(name);
                start();
            }
    
            @Override
            public void run() {
                try {
                    while (true){
                        System.out.println(this);
                        if(--countDown==0)
                            return;
                        sleep(10);
                    }
                }catch (InterruptedException e){
                    System.out.println("interrupted");
                }
            }
    
            @Override
            public String toString() {
                return getName()+": "+countDown;
            }
        }
    
        public InnerThread1(String name){
            inner = new Inner(name);
        }
    }
    
    
    //命名内部类-Runnable
    class InnerRunnable1 {
        private int countDown = 5;
        private Inner inner;
        private class Inner implements Runnable{
            Thread t;
            Inner(String name){
                t = new Thread(this, name);
                t.start();
            }
    
            @Override
            public void run() {
                try {
                    while (true){
                        System.out.println(this);
                        if(--countDown==0) return;
                        TimeUnit.SECONDS.sleep(10);
                    }
                }catch (InterruptedException e){
                    System.out.println("interrupted");
                }
            }
    
            @Override
            public String toString() {
                return t.getName()+": "+countDown;
            }
        }
    
        public InnerRunnable1(String name){
            inner = new Inner(name);
        }
    }
    
    //匿名内部类-Thread
    class InnerThread2 {
        private int countDown = 5;
        public InnerThread2(final String name){
              new Thread(name){
                  @Override
                  public void run() {
                      try {
                          while (true){
                              System.out.println(this);
                              if(--countDown==0)
                                  return;
                              sleep(10);
                          }
                      }catch (InterruptedException e){
                          System.out.println("interrupted");
                      }
                  }
    
                  @Override
                  public String toString() {
                      return getName()+": "+countDown;
                  }
              }.start();
        }
    }
    
    //匿名内部类-Runnable2
    class InnerRunnable2 {
        private int countDown = 5;
        public InnerRunnable2(final String name){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        while (true){
                            System.out.println(this);
                            if(--countDown==0)
                                return;
                            TimeUnit.SECONDS.sleep(10);
                        }
                    }catch (InterruptedException e){
                        System.out.println("interrupted");
                    }
                }
    
                @Override
                public String toString() {
                    return Thread.currentThread().getName()+": "+countDown;
                }
            }, name).start();
        }
    }
    
    //a separate methid to run somoe code as task
    class ThreadMethod{
        private int countDown = 5;
        private Thread thread;
        private String name;
    
        public ThreadMethod(String name) {
            this.name = name;
        }
    
        public void runTask(){
            if (null==thread){
                new Thread(name){
                    @Override
                    public void run() {
                        try {
                            while (true){
                                System.out.println(this+"");
                                if(--countDown==0) return;
                                sleep(10);
                            }
                        }catch (InterruptedException e){
                            System.out.println("interrupted");
                        }
                    }
    
                    @Override
                    public String toString() {
                        return Thread.currentThread().getName()+": "+countDown;
                    }
                }.start();
            }
        }
    }
    
    public class Test2{
        public static void main(String[] args) {
                new InnerThread1("InnerThread1");
                new InnerRunnable1("InnerRunnable1");
                new InnerThread2("InnerThread2");
                new InnerRunnable2("InnerRunnable2");
                new ThreadMethod("ThreadMethod");
        }
    }

    输出结果:

    InnerThread1: 5
    InnerThread2: 5
    InnerRunnable1: 5
    InnerRunnable2: 5
    InnerThread1: 4
    InnerThread2: 4
    InnerThread2: 3
    InnerThread1: 3
    InnerThread1: 2
    InnerThread2: 2
    InnerThread1: 1
    InnerThread2: 1
    InnerRunnable1: 4
    InnerRunnable2: 4
    InnerRunnable1: 3
    InnerRunnable2: 3
    InnerRunnable2: 2
    InnerRunnable1: 2
    InnerRunnable2: 1
    InnerRunnable1: 1
    
    Process finished with exit code 0
  • 相关阅读:
    插值法——线性分段插值
    插值法——lagrange插值
    插值法——多项式插值
    方程求根——二分法
    插值法——三次样条插值
    方程求根——两种加速迭代法
    线性方程组的迭代解法——超松弛迭代法
    线性方程组的迭代解法——共轭梯度法
    线性方程组的迭代解法——最速下降法
    Essential COM学习体会
  • 原文地址:https://www.cnblogs.com/soar-hu/p/6734702.html
Copyright © 2020-2023  润新知