Here you can find the source of run(final Runnable runnable, long timeout, TimeUnit unit)
public static boolean run(final Runnable runnable, long timeout, TimeUnit unit)
//package com.java2s; /*//from ww w. j a v a 2s .c om * Copyright (c) 2014, the Dart project authors. * * Licensed under the Eclipse Public License v1.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.eclipse.org/legal/epl-v10.html * * 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. */ import com.google.common.util.concurrent.Uninterruptibles; import java.util.concurrent.TimeUnit; public class Main { /** * Executes given {@link Runnable} with timeout. * * @return {@code true} if the {@link Runnable} finished within the timeout. */ public static boolean run(final Runnable runnable, long timeout, TimeUnit unit) { final boolean done[] = { false }; Thread thread = new Thread("TimeboxUtils.run") { @Override public void run() { runnable.run(); done[0] = true; } }; thread.start(); Uninterruptibles.joinUninterruptibly(thread, timeout, unit); return done[0]; } }