Java Thread Executor Execute executeAndWait(final Runnable r)

Here you can find the source of executeAndWait(final Runnable r)

Description

execute And Wait

License

Apache License

Declaration

public static void executeAndWait(final Runnable r) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class Main {
    private static Executor exec = Executors.newSingleThreadExecutor();

    public static void executeAndWait(final Runnable r) {
        final CountDownLatch countDown = new CountDownLatch(1);
        Runnable task = new Runnable() {

            public void run() {

                try {
                    r.run();//  ww w  .j av  a2 s .c o  m
                } finally {
                    System.err.println("scheduled");
                    countDown.countDown();
                }

            }
        };

        exec.execute(task);

        try {
            countDown.await();
        } catch (InterruptedException e) {

            e.printStackTrace();
            return;
        }

    }
}

Related

  1. execute(Runnable command)
  2. execute(Runnable runnable)
  3. execute(Runnable task)
  4. execute(Runnable task)
  5. execute(String name, Runnable runnable)
  6. executeAsynchronously(Runnable r)
  7. executeForever(final Runnable runnable)
  8. executeFuture(Callable callable)
  9. executeInCachedPool(Runnable runnable)