/** * Starts a scheduled thread that periodically calls {@link SparkSession#triggerTimeout(long)} * on each {@link SparkSession} managed by this class. */ private void startTimeoutThread() { long sessionTimeout = conf.getTimeVar(HiveConf.ConfVars.SPARK_SESSION_TIMEOUT, TimeUnit.MILLISECONDS); long sessionTimeoutPeriod = conf.getTimeVar(HiveConf.ConfVars.SPARK_SESSION_TIMEOUT_PERIOD, TimeUnit.MILLISECONDS); ScheduledExecutorService es = Executors.newSingleThreadScheduledExecutor(); // Schedules a thread that does the following: iterates through all the active SparkSessions // and calls #triggerTimeout(long) on each one. If #triggerTimeout(long) returns true, then // the SparkSession is removed from the set of active sessions managed by this class. timeoutFuture = es.scheduleAtFixedRate(() -> createdSessions.stream() .filter(sparkSession -> sparkSession.triggerTimeout(sessionTimeout)) .forEach(createdSessions::remove), 0, sessionTimeoutPeriod, TimeUnit.MILLISECONDS); } }