Async.java

  1. /**
  2.  *
  3.  * Copyright 2014-2018 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.util;

  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;

  20. public class Async {

  21.     /**
  22.      * Creates a new thread with the given Runnable, marks it daemon, starts it and returns the started thread.
  23.      *
  24.      * @param runnable TODO javadoc me please
  25.      * @return the started thread.
  26.      */
  27.     public static Thread go(Runnable runnable) {
  28.         Thread thread = daemonThreadFrom(runnable);
  29.         thread.start();
  30.         return thread;
  31.     }

  32.     /**
  33.      * Creates a new thread with the given Runnable, marks it daemon, sets the name, starts it and returns the started
  34.      * thread.
  35.      *
  36.      * @param runnable TODO javadoc me please
  37.      * @param threadName the thread name.
  38.      * @return the started thread.
  39.      */
  40.     public static Thread go(Runnable runnable, String threadName) {
  41.         Thread thread = daemonThreadFrom(runnable);
  42.         thread.setName(threadName);
  43.         thread.start();
  44.         return thread;
  45.     }

  46.     public static Thread daemonThreadFrom(Runnable runnable) {
  47.         Thread thread = new Thread(runnable);
  48.         thread.setDaemon(true);
  49.         return thread;
  50.     }

  51.     /**
  52.      * Like {@link Runnable}, but allows the <code>runOrThrow()</code> method to throw an exception.
  53.      * <p>
  54.      * If the exception is an instance of {@link RuntimeException}, then it will be re-thrown, otherwise <b>it will be
  55.      * simply logged.</b>
  56.      */
  57.     public abstract static class ThrowingRunnable implements Runnable {

  58.         public static final Logger LOGGER = Logger.getLogger(ThrowingRunnable.class.getName());

  59.         @Override
  60.         public final void run() {
  61.             try {
  62.                 runOrThrow();
  63.             }
  64.             catch (Exception e) {
  65.                 if (e instanceof RuntimeException) {
  66.                     throw (RuntimeException) e;
  67.                 }
  68.                 LOGGER.log(Level.WARNING, "Caught Exception", e);
  69.             }
  70.         }

  71.         public abstract void runOrThrow() throws Exception;
  72.     }
  73. }