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 long time(Executor executor, int concurrency, final Runnable action) throws InterruptedException {
    final CountDownLatch ready = new CountDownLatch(concurrency);
    final CountDownLatch start = new CountDownLatch(1);
    final CountDownLatch done = new CountDownLatch(concurrency);

    for (int i = 0; i < concurrency; i++) {
        executor.execute(new Runnable() {
            @Override//from   w  w w .  j a  v a 2  s .c o m
            public void run() {
                ready.countDown(); // Tell timer we're ready
                try {
                    start.await(); // Wait till peers are ready
                    action.run();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                } finally {
                    done.countDown(); // Tell timer we're done
                }
            }
        });
    }

    ready.await(); // Wait for all workers to be ready
    long startNanos = System.nanoTime();
    start.countDown(); // And they're off!
    done.await(); // Wait for all workers to finish
    return System.nanoTime() - startNanos;
}

From source file:Main.java

/**
 * Runs a piece of code after the next layout run
 * /*  w w  w  . j a  v a 2 s  .c  o m*/
 * @param view The {@link View} used.
 * @param runnable The {@link Runnable} used after the next layout run
 */
@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 */
            // aldenml: The new api is just the same logic
            //if (hasJellyBean()) {
            //    view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            //} else {
            view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            //}
            runnable.run();
        }
    };
    view.getViewTreeObserver().addOnGlobalLayoutListener(listener);
}

From source file:org.apache.jena.rdfconnection.RDFConnectionRemote.java

/** Convert HTTP status codes to exceptions */
static protected void exec(Runnable action) {
    try {/*from  ww  w .  j  a va 2s.  c  o  m*/
        action.run();
    } catch (HttpException ex) {
        handleHttpException(ex, false);
    }
}

From source file:Main.java

public static void fadeAnimation(final View view, final float fromAlpha, final float toAlpha, long duration,
        final Runnable callback) {
    Animation anim = new AlphaAnimation(fromAlpha, toAlpha);
    anim.setDuration(duration);//w  ww.  jav a  2s . c  om
    anim.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            view.setAlpha(toAlpha);
            if (toAlpha > 0)
                view.setVisibility(View.VISIBLE);
            else
                view.setVisibility(View.GONE);

            if (callback != null)
                callback.run();
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }
    });

    view.setVisibility(View.VISIBLE);
    view.startAnimation(anim);
}

From source file:Main.java

public static void showWifiSettings(final Activity activity, final Runnable yes, final Runnable no) {
    // check for internet connection
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
    alertDialog.setTitle("No Internet Connection");
    alertDialog.setMessage("Would you like to change settings ?");
    // Setting Positive "Yes" Button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            activity.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
            if (yes != null)
                yes.run();
        }/*from  w w w  . jav  a 2 s .  c o m*/
    });
    // Setting Negative "NO" Button
    alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
            if (no != null)
                no.run();
        }
    });
    // Showing Alert Message
    alertDialog.show();
}

From source file:Main.java

public static void invokeAll(final List<Runnable> tasks, final ExecutorService executor)
        throws InterruptedException, ExecutionException {
    ExecutionException saved = null;

    if (executor != null) {
        final List<Future<?>> futures = new ArrayList<>();
        for (final Runnable task : tasks)
            futures.add(executor.submit(task));

        for (final Future<?> future : futures) {
            try {
                future.get();/*w w  w . j ava 2s .  com*/
            } catch (InterruptedException e) {
                throw e;
            } catch (ExecutionException e) {
                if (saved == null) {
                    saved = e;
                } else {
                    saved.addSuppressed(e);
                }
            }
        }
    } else {
        for (final Runnable task : tasks) {
            try {
                task.run();
            } catch (Exception e) {
                if (saved == null) {
                    saved = new ExecutionException(e);
                } else {
                    saved.addSuppressed(e);
                }
            }
        }
    }

    if (saved != null)
        throw saved;
}

From source file:Main.java

public static CountDownLatch execute(int threadCount, final Runnable task) {
    final CountDownLatch startSignal = new CountDownLatch(1);
    final CountDownLatch startedSignal = new CountDownLatch(threadCount);
    final CountDownLatch doneSignal = new CountDownLatch(threadCount);
    for (int i = 0; i < threadCount; i++) {
        Thread t = new Thread() {
            public void run() {
                startedSignal.countDown();
                try {
                    startSignal.await();
                } catch (InterruptedException e) {
                    //ignore
                }//from  w  w  w.jav a2 s. c  om

                try {
                    task.run();
                } finally {
                    doneSignal.countDown();
                }

            }
        };

        t.start();
    }

    try {
        startedSignal.await();
    } catch (InterruptedException e) {
        //ignore
    }
    startSignal.countDown();
    return doneSignal;
}

From source file:Main.java

public static void yesNoMessage(final Activity activity, final String title, final String body,
        final String yesButtonLabel, final String noButtonLabel, final Runnable yesRunnable,
        final Runnable noRunnable) {
    activity.runOnUiThread(new Runnable() {
        @Override//  ww w  .j a  v a2  s .  co  m
        public void run() {

            AlertDialog.Builder dialog = new AlertDialog.Builder(activity);

            dialog.setTitle(title);
            dialog.setMessage(body);

            dialog.setPositiveButton(yesButtonLabel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    if (yesRunnable != null)
                        yesRunnable.run();
                }
            });

            dialog.setNegativeButton(noButtonLabel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    if (noRunnable != null)
                        noRunnable.run();
                }
            });

            dialog.create();
            dialog.show();
        }
    });
}

From source file:Main.java

public static void linearAnimation(final View view, final int startX, final int startY, final int endX,
        final int endY, long duration, final Runnable callback) {
    Animation anim = new TranslateAnimation(startX, endX, startY, endY);
    anim.setDuration(duration);/*w  ww .  ja v  a  2s  .  co  m*/
    anim.setInterpolator(new DecelerateInterpolator());
    view.startAnimation(anim);

    anim.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            view.clearAnimation();
            view.setX(view.getX() + endX);
            view.setY(view.getY() + endY);

            if (callback != null)
                callback.run();
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }
    });
}

From source file:com.jhash.oimadmin.Utils.java

public static void executeAsyncOperation(String operationName, Runnable operation) {
    logger.debug("Setting up execution of {} in separate thread", operationName);
    Thread oimConnectionThread = threadFactory.newThread(new Runnable() {

        @Override//w  w  w  . j  a v a2  s.c  om
        public void run() {
            try {
                logger.debug("Trying to run operation {}", operationName);
                operation.run();
                logger.debug("Completed operation {}.", operationName);
            } catch (Exception exception) {
                logger.warn("Failed to run operation " + operationName, exception);
            }
        }
    });
    oimConnectionThread.setDaemon(true);
    oimConnectionThread.setName(operationName);
    oimConnectionThread.start();
    logger.debug("Completed setup of execution of {} in separate thread", operationName);
}