• 在方法中定义一个类的写法


    在《JAVA并发编程实战》这本书里,看到一种以前没有见过的写法,就是在方法里面定义一个类,这种写法太少见了,而且搜索的话也没有什么博客介绍过。在此记录一下书中给出的代码,是符合java的语法的

    public class TimedRun2 {
        private static final ScheduledExecutorService cancelExec = newScheduledThreadPool(1);
    
        public static void timedRun(final Runnable r,
                                    long timeout, TimeUnit unit)
                throws InterruptedException {
            class RethrowableTask implements Runnable {
                private volatile Throwable t;
    
                public void run() {
                    try {
                        r.run();
                    } catch (Throwable t) {
                        this.t = t;
                    }
                }
    
                void rethrow() {
                    if (t != null)
                        throw launderThrowable(t);
                }
            }
    
            RethrowableTask task = new RethrowableTask();
            final Thread taskThread = new Thread(task);
            taskThread.start();
            cancelExec.schedule(new Runnable() {
                public void run() {
                    taskThread.interrupt();
                }
            }, timeout, unit);
            taskThread.join(unit.toMillis(timeout));
            task.rethrow();
        }
    }
  • 相关阅读:
    python学习手册 (第3版)
    服务器搭建
    阿里云 大数据 云计算 分布式
    PS插件开发plugin
    GIS九交模型
    人脸识别 人工智能(AI)
    Github上发布托管和下载
    RPLiDAR 激光雷达探测地面高程
    linux内核调试
    convex hull
  • 原文地址:https://www.cnblogs.com/skyvalley/p/15362726.html
Copyright © 2020-2023  润新知