Example usage for java.lang Runnable run

List of usage examples for java.lang Runnable run

Introduction

In this page you can find the example usage for java.lang Runnable run.

Prototype

public abstract void run();

Source Link

Document

When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.

Usage

From source file:Main.java

public static void executeOnGlobalLayout(View view, final Runnable runnable) {
    final WeakReference<View> viewReference = new WeakReference<>(view);
    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override//from  w w  w . j  a  va2s  .c  o m
        public void onGlobalLayout() {
            removeOnGlobalLayoutListener(viewReference.get(), this);
            runnable.run();
        }
    });

}

From source file:Main.java

public static Dialog backgroundProcess(Context context, final Runnable run, final boolean showDialog,
        String loadingComment) {/*from www .  j ava2s  .c om*/
    final ProgressDialog progressdialog = creativeProgressBar(context, loadingComment);
    if (showDialog)
        progressdialog.show();
    Runnable wrapper = new Runnable() {
        public void run() {
            run.run();
            if (showDialog)
                progressdialog.dismiss();
        }
    };
    Thread thread = new Thread(wrapper);
    thread.setDaemon(true);
    thread.start();
    return progressdialog;
}

From source file:com.zoterodroid.client.NetworkUtilities.java

/**
 * Executes the network requests on a separate thread.
 * // w w w .  j  a v  a2 s  .c om
 * @param runnable The runnable instance containing network mOperations to
 *        be executed.
 */
public static Thread performOnBackgroundThread(final Runnable runnable) {
    final Thread t = new Thread() {
        @Override
        public void run() {
            try {
                runnable.run();
            } finally {
            }
        }
    };
    t.start();
    return t;
}

From source file:Main.java

public static void scale(final View view, float fromScale, float toScale, long duration,
        final Runnable whenDone) {
    if (Build.VERSION.SDK_INT >= 12) {
        if (duration == 0) {
            view.setScaleX(toScale);/*from w ww .j  a v a2 s  .c om*/
            view.setScaleY(toScale);
            if (whenDone != null)
                whenDone.run();
        } else {
            ViewPropertyAnimator animation = view.animate().scaleX(toScale).scaleY(toScale)
                    .setDuration(duration);
            if (whenDone != null) {
                animation.setListener(new AnimatorListener() {
                    @Override
                    public void onAnimationCancel(Animator animation) {
                        whenDone.run();
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        whenDone.run();
                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {
                    }

                    @Override
                    public void onAnimationStart(Animator animation) {
                    }

                });
            }
            animation.start();
        }
    } else {
        ScaleAnimation scale = new ScaleAnimation(fromScale, toScale, fromScale, toScale,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scale.setDuration(duration);
        scale.setFillEnabled(true);
        scale.setFillBefore(true);
        scale.setFillAfter(true);

        if (whenDone != null) {
            scale.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationEnd(Animation animation) {
                    whenDone.run();
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }

                @Override
                public void onAnimationStart(Animation animation) {
                }

            });
        }
        addAnimation(view, scale);
    }
}

From source file:Main.java

public static void edtSmartInvokeAndWait(Runnable block) {
    if (!SwingUtilities.isEventDispatchThread())
        try {// w  w  w.  j  av a  2  s .c  o  m
            SwingUtilities.invokeAndWait(block);
        } catch (InterruptedException e) {
        } catch (InvocationTargetException e) {
            if (e.getCause() instanceof RuntimeException)
                throw (RuntimeException) e.getCause();
        }
    else
        block.run();
}

From source file:Main.java

/**
 * Runs a piece of code after the next layout run
 *
 * @param view     The {@link View} used.
 * @param runnable The {@link Runnable} used after the next layout run
 *///  w w  w . ja v a 2 s  .  c  om
@SuppressLint("NewApi")
public static void doAfterLayout(final View view, final Runnable runnable) {
    final OnGlobalLayoutListener listener = new OnGlobalLayoutListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onGlobalLayout() {
            /* Layout pass done, unregister for further events */
            view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            runnable.run();
        }
    };
    view.getViewTreeObserver().addOnGlobalLayoutListener(listener);
}

From source file:com.raphfrk.craftproxyclient.gui.GUIManager.java

public static JSONObject getNewLoginDetails() {
    final AtomicReference<LoginDialog> login = new AtomicReference<LoginDialog>();
    Runnable r = (new Runnable() {
        public void run() {
            login.set(new LoginDialog(CraftProxyClient.getGUI()));
            login.get().setVisible(true);
            login.get().dispose();/*from   w w w  . ja  v a2 s .c o  m*/
        }
    });
    if (SwingUtilities.isEventDispatchThread()) {
        r.run();
    } else {
        try {
            SwingUtilities.invokeAndWait(r);
        } catch (InvocationTargetException | InterruptedException e) {
            return null;
        }
    }
    return AuthManager.authAccessToken(login.get().getEmail(), login.get().getPassword());
}

From source file:tech.sirwellington.alchemy.http.HttpVerbImplTest.java

private static long time(Runnable task) {
    long start = currentTimeMillis();
    task.run();
    long end = currentTimeMillis();
    return end - start;
}

From source file:org.fuin.esc.eshttp.ESHttpEventStoreIT.java

private static void executeMultipleAndWaitFor(final Runnable runnable, final Supplier<Boolean> finished,
        final int maxTries) {
    int tries = 0;
    do {//ww  w . ja va 2s .c  o m
        runnable.run();
        if (!finished.get()) {
            sleep(100);
            tries++;
        }
    } while (!finished.get() || (tries == maxTries));
    if (!finished.get()) {
        throw new IllegalStateException("Waiting for result failed!");
    }
}

From source file:Main.java

/**
 * Creates a single thread executor which ignores all executions that occur
 * while it is busy executing a Runnable. This is useful for tasks that may
 * be requested multiple times from multiple sources, but which only need to
 * take place once./*from www . j a  v  a 2s.c o  m*/
 * 
 * @param name the name for threads created within this pool
 */
@SuppressWarnings("serial")
public static ExecutorService newCoalescingThreadPool(String name) {
    return new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(),
            newNamedThreadFactory(name)) {
        private boolean executing = false;

        @Override
        public void execute(final Runnable command) {
            synchronized (this) {
                if (executing)
                    return;
                executing = true;
            }
            super.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        command.run();
                    } finally {
                        executing = false;
                    }
                }
            });
        }
    };
}