判断方法是否运行在指定线程中

判断某个方法是否运行在指定线程中:

方法位置:  org.apache.flink.runtime.rpc.MainThreadValidatorUtil#isRunningInExpectedThread

	/**
	 * Returns true iff the current thread is equals to the provided expected thread and logs violations.
	 *
	 * @param expected the expected main thread.
	 * @return true iff the current thread is equals to the provided expected thread.
	 */
	public static boolean isRunningInExpectedThread(@Nullable Thread expected) {
		Thread actual = Thread.currentThread();
		if(expected != actual) {
			String violationMsg = "Violation of main thread constraint detected: expected <" + expected + "> but running in <" + actual + ">.";
			LOG.warn(violationMsg, new Exception(violationMsg));
			return false;
		}
		return true;
	}

调用方法:

	/**
	 * Validates that the method call happens in the RPC endpoint's main thread.
	 *
	 * 

IMPORTANT: This check only happens when assertions are enabled, * such as when running tests. * *

This can be used for additional checks, like *

{@code
	 * protected void concurrencyCriticalMethod() {
	 *     validateRunsInMainThread();
	 *
	 *     // some critical stuff
	 * }
	 * }
*/ public void validateRunsInMainThread() { assert MainThreadValidatorUtil.isRunningInExpectedThread(currentMainThread.get()); }

你可能感兴趣的:(Java,大数据,java,前端,javascript)