/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.runtime.concurrent;

import javax.annotation.Nonnull;

import java.util.concurrent.ScheduledExecutorService;

/**
 * Adapter class for a {@link ScheduledExecutorService} which shall be used as a
 * {@link ComponentMainThreadExecutor}. It enhances the given executor with an assert that the current thread is the
 * main thread of the executor.
 */
public class ComponentMainThreadExecutorServiceAdapter
	extends ScheduledExecutorServiceAdapter implements ComponentMainThreadExecutor {

	/** A runnable that should assert that the current thread is the expected main thread. */
	@Nonnull
	private final Runnable mainThreadCheck;

	public ComponentMainThreadExecutorServiceAdapter(
		@Nonnull ScheduledExecutorService scheduledExecutorService,
		@Nonnull Runnable mainThreadCheck) {
		super(scheduledExecutorService);
		this.mainThreadCheck = mainThreadCheck;
	}

	@Override
	public void assertRunningInMainThread() {
		mainThreadCheck.run();
	}
}

@@ -857,87 +857,301 @@
 ort 
-javax.annotation.Nonnull;%0A%0Aimport java.util.concurrent.ScheduledExecutorService
+org.apache.flink.runtime.rpc.MainThreadValidatorUtil;%0A%0Aimport java.util.concurrent.Callable;%0Aimport java.util.concurrent.ScheduledExecutorService;%0Aimport java.util.concurrent.ScheduledFuture;%0Aimport java.util.concurrent.TimeUnit;%0A%0Aimport static org.apache.flink.util.Preconditions.checkNotNull
 ;%0A%0A/
@@ -1208,16 +1208,45 @@
 Service%7D
+ or %7B@link ScheduledExecutor%7D
  which s
@@ -1474,49 +1474,8 @@
 pter
-%0A%09extends ScheduledExecutorServiceAdapter
  imp
@@ -1511,16 +1511,69 @@
 ecutor %7B
+%0A%0A%09private final ScheduledExecutor scheduledExecutor;
 %0A%0A%09/** A
@@ -1661,18 +1661,8 @@
  */%0A
-%09@Nonnull%0A
 %09pri
@@ -1756,114 +1756,610 @@
 (%0A%09%09
-@Nonnull ScheduledExecutorService scheduledExecutorService,%0A%09%09@Nonnull Runnable mainThreadCheck) %7B%0A%09%09super
+%09final ScheduledExecutorService scheduledExecutorService,%0A%09%09%09final Runnable mainThreadCheck) %7B%0A%09%09this(new ScheduledExecutorServiceAdapter(scheduledExecutorService), mainThreadCheck);%0A%09%7D%0A%0A%09public ComponentMainThreadExecutorServiceAdapter(%0A%09%09%09final ScheduledExecutor scheduledExecutorService,%0A%09%09%09final Thread mainThread) %7B%0A%09%09this(scheduledExecutorService, () -%3E MainThreadValidatorUtil.isRunningInExpectedThread(mainThread));%0A%09%7D%0A%0A%09private ComponentMainThreadExecutorServiceAdapter(%0A%09%09%09final ScheduledExecutor scheduledExecutor,%0A%09%09%09final Runnable mainThreadCheck) %7B%0A%09%09this.scheduledExecutor = checkNotNull
 (sch
@@ -2372,23 +2372,16 @@
 Executor
-Service
 );%0A%09%09thi
@@ -2400,16 +2400,29 @@
 Check = 
+checkNotNull(
 mainThre
@@ -2428,16 +2428,17 @@
 eadCheck
+)
 ;%0A%09%7D%0A%0A%09@
@@ -2516,11 +2516,950 @@
 un();%0A%09%7D
+%0A%0A%09@Override%0A%09public ScheduledFuture%3C?%3E schedule(final Runnable command, final long delay, final TimeUnit unit) %7B%0A%09%09return scheduledExecutor.schedule(command, delay, unit);%0A%09%7D%0A%0A%09@Override%0A%09public %3CV%3E ScheduledFuture%3CV%3E schedule(final Callable%3CV%3E callable, final long delay, final TimeUnit unit) %7B%0A%09%09return scheduledExecutor.schedule(callable, delay, unit);%0A%09%7D%0A%0A%09@Override%0A%09public ScheduledFuture%3C?%3E scheduleAtFixedRate(final Runnable command, final long initialDelay, final long period, final TimeUnit unit) %7B%0A%09%09return scheduledExecutor.scheduleAtFixedRate(command, initialDelay, period, unit);%0A%09%7D%0A%0A%09@Override%0A%09public ScheduledFuture%3C?%3E scheduleWithFixedDelay(final Runnable command, final long initialDelay, final long delay, final TimeUnit unit) %7B%0A%09%09return scheduledExecutor.scheduleWithFixedDelay(command, initialDelay, delay, unit);%0A%09%7D%0A%0A%09@Override%0A%09public void execute(final Runnable command) %7B%0A%09%09scheduledExecutor.execute(command);%0A%09%7D
 %0A%7D%0A

Make ComponentMainThreadExecutorServiceAdapter accept ScheduledExecutor